问题
I have an ASP.NET MVC application, and I'm making use of EntityFramework 5.0. I've also installed a 'CodeFirstMembershipProvider' Nuget package, which provided some code for ASP.NET membership - and I believe also made changes to my web.config.
Basically, my problem is that my application works fine on a local SQL Server instance, but when I specify a remote DB using this connection string:
<add name="DefaultConnection" providerName="System.Data.SqlClient" connectionString="Data Source=*******************, 14330;Integrated Security=False;Database=qrcodetr_database;Uid=qrcodetr_admin;Password=*********; Connect Timeout=15;Encrypt=False;Network Library=dbmssocn;Packet Size=4096" />
(password and hostname edited out)
it fails to use this connection string. It continues to attempt to use the local SQL Server instance instead - so it works locally but when I try it on my web and database host it fails - since it tries to use a local SQL server instance which it can't do on my database host. The remote database host uses SQL Server 2008 R2.
Does anyone know why this might be or what I can do to solve this?
I'm not going to post the whole web.config, but here's a few lines that might be relevant:
<add key="DatabaseInitializerForType TreasureHuntDB, QrCodeTreasureHunter" value="DataContextInitializer, QrCodeTreasureHunter"
<sessionState mode="InProc" customProvider="DefaultSessionProvider"> <providers> <add name="DefaultSessionProvider" type="System.Web.Providers.DefaultSessionStateProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" /> </providers> </sessionState> <membership defaultProvider="CodeFirstMembershipProvider"> <providers> <add name="CodeFirstMembershipProvider" type="CodeFirstMembershipProvider" connectionStringName="DefaultConnection" /> </providers> </membership> <roleManager enabled="true" defaultProvider="CodeFirstRoleProvider"> <providers> <clear /> <add name="CodeFirstRoleProvider" type="CodeFirstRoleProvider" connectionStringName="DefaultConnection" /> </providers> </roleManager>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
</entityFramework>
I'm able to successfully connect to the remote DB via SQL Server 2008, by the way, so the issue definitely seems to be a configuration issue on my end.
By the way, I am using a DataContextInitializer which is set to 'CreateDatabaseIfNotExists'. I have set it to this because on the hosted database, I don't have permissions to CREATE or DROP the database, so what I've done is I've created a backup of one of the ones created locally (in SQL Server Express) and used the restore feature to restore the file to my hosted database. So I have a database in place, it just won't work with my application.
回答1:
You don't seem to tell the EF the name of the connection string so it is just creating one by convention. You can either pass the name to the DbContext ctor in the form of "Name=DefaultConnection"
or rename the connection string in your config so that its name matches the name of the DbContext derived class.
回答2:
This question and answer suggests that your connection string section should look like this:
<connectionStrings>
<clear /> <!--- need this to prevent using LocalSqlServer from machine.config or somewhere becouse it is not present when when publish to hosting -->
<add name="DefaultConnection" providerName="System.Data.SqlClient" connectionString="etc" />
</connectionStrings>
来源:https://stackoverflow.com/questions/14696589/connection-string-to-remote-db-when-using-entityframework