Entity Framework 到远程服务器(连接字符串)

Entity Framework To A Remote Server (Connection String)

我已经在我的所有项目上安装了 EF 版本 6.1.3。然后我创建了一个模型 class、一个上下文 class,并在我的本地计算机上安装了一个 MSSQL 数据库(我没有做任何其他事情)。一切都非常完美(它以某种方式知道我的本地数据库)。

型号class:

public class Account
    {
        public int Id { get; set; }
        public string Name{ get; set; }
    }

数据上下文class:

public class MyClassDataContext: DbContext
    {
        public DbSet<Account> Accounts{ get; set; }
    }

App.config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=xxxxxx" requirePermission="false" />
  </configSections>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
</configuration>

然后我尝试将它移动到远程数据库,但它不起作用。我尝试了一切。

完成工作的正确方法是什么?

编辑: 我试过这个连接字符串,但没有任何反应。该应用仍会尝试连接本地数据库。

<connectionStrings>
    <add name="MyClassDataContext" connectionString="Data Source=MyRemoteServer;Initial Catalog=MyRemoteCatalog;Integrated Security=true" providerName="System.Data.SqlClient"/>
  </connectionStrings>
  <configSections>

您需要创建确保您的连接字符串的 name 是您的上下文的完全限定名称,或者为您的上下文创建一个显式默认构造函数。由于您在评论中提到了它,因此您提供的 link 不适合您,因为您使用的是代码优先。请尝试 this link

下面是一个功能齐全的控制台应用程序,可以演示它应该如何工作,以及配置文件。这将使用我们的本地 SQLServer 安装,但 不是 SQLExpress。它也应该适用于任何远程数据库。

请注意,在我之前发布的应用配置中,我将连接字符串部分放在了顶部。这是不正确的:configSections 必须 是第一个节点。

namespace TestApp
{
    public class Account
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }

    public class MyClassDataContext : DbContext
    {
        public DbSet<Account> Accounts { get; set; }
    }

    class Program
    {    
        static void Main(string[] args)
        {
            using (var x = new MyClassDataContext())
            {
                x.Accounts.Add(new Account { Name = "Drew" });
                x.SaveChanges();

                var y = x.Accounts;
                foreach (var s in y)
                {
                    Console.WriteLine(s.Name);
                }
            }

            Console.ReadKey();
        }
    }
}

配置文件:

<configuration> 
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <connectionStrings>
    <add name="ConsoleApplication4.MyClassDataContext" connectionString="Data Source=.;Initial Catalog=MyClass;Integrated Security=true" providerName="System.Data.SqlClient"/>
  </connectionStrings>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
  </startup>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="mssqllocaldb" />
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>  
</configuration>