且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用的EntityFramework当连接字符串到远程数据库

更新时间:2023-11-15 11:30:40

您似乎没有告诉EF连接字符串的名称,以便它只是创造一个又约定。您可以通过名字到的DbContext构造函数在NAME = DefaultConnection的形式或重命名你的配置连接字符串,这样它的名字的DbContext的名称相匹配派生类。

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.

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.