Hy!
I'm new to ASP.NET MVC 5. I try to change the default database name. But I don't know how. My goal is to have only one database for the whole application.
My actual working Web.Config:
<connectionStrings>
<add name="DefaultConnection" connectionString="DataSource=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-SqlTest-20131122100021.mdf;Initial Catalog=aspnet-SqlTest-20131122100021;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
This Web.Config file should look like this and should work ^^ (actual I get a error that the database could not be created and the physical name may be incorrect)
<connectionStrings>
<add name="DefaultConnection" connectionString="DataSource=(LocalDb)\v11.0;AttachDbFilename=Example.mdf;Initial Catalog=Example;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
Thanks for help :)
You need to add |DataDirectory|\ before Example.mdf, like below:
<connectionStrings>
<add name="DefaultConnection"
connectionString="DataSource=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\Example.mdf;Initial Catalog=Example;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
Related
I have been looking and reading, and I could not find this solution yet.
I have a small ASP.NET MVC project I am working with two other developers, we have the same settings, just the computer name is different.
So, our connection strings section in web.config looks something like this:
<!-- Hugo
<add name="DefaultConnection" connectionString="data source=DEKSTOP123\DATABASE;..."/>
<add name="OtherConnection" connectionString="metadata=res:...;data source=DESKTOP123\DATABASE;..."/>
-->
<!-- Paco -->
<add name="DefaultConnection" connectionString="data source=DEKSTOP456\DATABASE;..."/>
<add name="OtherConnection" connectionString="metadata=res:...;data source=DESKTOP456\DATABASE;..."/>
<!-- Luis
<add name="DefaultConnection" connectionString="data source=DEKSTOP789\DATABASE;..."/>
<add name="OtherConnection" connectionString="metadata=res:...;data source=DESKTOP789\DATABASE;..."/>
-->
We just comment and uncomment whatever connections is ours, but it's messy. Is there a better solution for this?
I see that we can use HttpContext.Current.IsDebuggingEnabled in controllers to check if debug=true is enabled in web.config. How can I check the same in startup.auth? I need to set different app Ids for my local and production environments for fb authentication using this condition. Please suggest if there is a better way to do this.
I would recommend creating a web.config entry for whatever dynamic data you need to set and then use web.config transforms to change the data depending on environment. This is very commonly used with connection strings to set different source databases depending on what environment youre in (local vs test vs prod).
See the example below:
local Web.config:
<connectionStrings>
<add name="myConnection" connectionString="Data Source=localhost;Initial Catalog=myDatabase;Integrated Security=True;" providerName="System.Data.SqlClient" />
</connectionStrings>
Web.Debug.Config:
<connectionStrings>
<add xdt:Transform="SetAttributes" xdt:Locator="Match(name)" name="myConnection" connectionString="Data Source=myTestSqlServer;Initial Catalog=myTestDb;User Id=myTestUserId;Password=myTestPassword;" providerName="System.Data.SqlClient" />
</connectionStrings>
Web.Release.Config:
<connectionStrings>
<add xdt:Transform="SetAttributes" xdt:Locator="Match(name)" name="myConnection" connectionString="Data Source=myProdSqlServer;Initial Catalog=myProdDb;User Id=myProdUserName;Password=myProdPassword;" providerName="System.Data.SqlClient" />
</connectionStrings>
I am trying to use the last connectionString with name sqlConString.
This is my connection string:
<connectionStrings>
<add name="DefaultConnection" providerName="System.Data.SqlClient" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=aspnet-saroh-20170305155508;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnet-saroh-20170305155508.mdf" />
<add name="crowdfundingEntities4" connectionString="metadata=res://*/Models.Model1.csdl|res://*/Models.Model1.ssdl|res://*/Models.Model1.msl;provider=System.Data.SqlClient;provider connection string="data source=(LocalDB)\v11.0;attachdbfilename=|DataDirectory|\crowdfunding.mdf;integrated security=True;multipleactiveresultsets=True;application name=EntityFramework"" providerName="System.Data.EntityClient" />
<add name="userdataEntities6" connectionString="metadata=res://*/Models.MyModel.csdl|res://*/Models.MyModel.ssdl|res://*/Models.MyModel.msl;provider=System.Data.SqlClient;provider connection string="data source=(LocalDB)\v11.0;attachdbfilename=|DataDirectory|\userdata.mdf;integrated security=True;multipleactiveresultsets=True;application name=EntityFramework"" providerName="System.Data.EntityClient" />
<add name="sqlConString" connectionString="DataSource=(LocalDB)\v11.0;attachdbfilename=|DataDirectory|\crowdfunding.mdf;integrated security=True;multipleactiveresultsets=True;application name=EntityFramework""/>
</connectionStrings>
I think your problem is connection string Data Source has a space between the word but your setting is DataSource
so try
<add name="sqlConString" connectionString="Data Source=(LocalDB)\v11.0;attachdbfilename=|DataDirectory|\crowdfunding.mdf;integrated security=True;multipleactiveresultsets=True;application name=EntityFramework""/>
If that does not solve your problem, then try:
provide a providerName
if you are using normal ado .net then it should be providerName="System.Data.SqlClient" or
if using entity framework need to addproviderName="System.Data.EntityClient" with adding metadata part in connection string (refer to your userdataEntities6)
if you are using entity framework, a part from adding providerName and modifying connectionString to include metadata also try change " to '
more info about this, check out https://blogs.msdn.microsoft.com/rickandy/2008/12/09/explicit-connection-string-for-ef/
In the default sample appication in mvc3, Where do the details we fill in registration form gets stored?
<configuration>
<connectionStrings>
<add name="ApplicationServices"
connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true"
providerName="System.Data.SqlClient" />
</connectionStrings>
I did not find any aspnetdb.mdf file in app_data folder.
You need to click the 'show all files' option as it is hidden IIRC.
I am trying to use SimpleMembership in my MVC 3 application. However I get the following error:
System.ArgumentException was unhandled
by user code. Unable to find the
requested .Net Framework Data
Provider. It may not be installed.
I'm using EF and the framework is obviously there since my app works perfect without the SimpleMembership API
Here is the Set Up in the Web config file:
<connectionStrings>
<add name="ApplicationServices" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true" providerName="System.Data.SqlClient" />
<add name="SeniorProjectModelContainer" connectionString="metadata=res://*/Models.SeniorProjectModel.csdl|res://*/Models.SeniorProjectModel.ssdl|res://*/Models.SeniorProjectModel.msl;provider=System.Data.SqlClient;provider connection string="Data Source=GOGOTOPPY;Initial Catalog=SPMT;Integrated Security=True;MultipleActiveResultSets=True"" providerName="System.Data.EntityClient" />
</connectionStrings>
And heres how it looks in the in the Membership file in the App_Start folder:
WebSecurity.InitializeDatabaseConnection(connectionStringName: "SeniorProjectModelContainer", userTableName: "Users", userIdColumn: "UserId", userNameColumn: "UserName", autoCreateTables: true);
Any help would be greatly appreciated!
Update:
The quick work around was to take the metadata part out of the connection string and change the provider name to the SqlClient so it looks similar to this:
<connectionStrings>
<add name="Membership" connectionString="Data Source=serverName;Database=datebaseName;User ID=idName;Password=password;Trusted_Connection=False;Encrypt=True;" providerName="System.Data.SqlClient" />
</connectionStrings>
Is there any downside of the connection string looking like this?
Not sure how you installed simplemembership but try doing it the nuget way for example,
http://www.nuget.org/List/Packages/SimpleMembership.Mvc3