将实体框架连接到远程服务器(连接字符串)

5

我已经在所有项目中安装了EF 6.1.3版本。然后我创建了一个模型类和上下文类,并在我的本地计算机上安装了MSSQL数据库(我没有做其他任何事情)。一切都工作得非常完美(不知何故,它知道我的本地数据库)。

模型类:

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

DataContext 类:

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>

这里有一个例子链接。但是它对我不起作用。我没有那些元数据。但即使我将它们删除,我仍然会收到错误消息... - arnie
当我将EF最佳实践添加到配置文件时,我看不到连接字符串。 - DarkMakukudo
1个回答

3
你需要确保连接字符串的name是你的上下文的完全限定名称,或者为你的上下文创建一个显式的默认构造函数。由于你在评论中提到了它,所以你提供的链接对你来说不起作用,因为你正在使用代码优先。请尝试使用此链接。以下是一个完全功能的控制台应用程序,可以演示它应该如何工作,以及配置文件。这将使用我们本地的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>

只有在您想要使用不同于EF约定的连接字符串时才需要重写构造函数。EF使用约定来查找连接字符串,因此它正在寻找名为<fullyqualifiednamespace>.DivAppsDataContext的连接字符串。但是它没有找到,所以它会回退到machine.config中的默认LocalSqlServer字符串。 - Erik Funkenbusch
我尝试使用构造函数和命名约定的方式,但是我在这里缺少什么? - arnie
@ErikFunkenbusch 谢谢提醒,我已经进行了编辑以表明这一点。arnie,我发布了一个测试控制台应用程序,成功连接,请尝试并查看它是否适用于您。 - DrewJordan
@DrewJordan 它有效了。你是对的,我采用了代码优先的方法。非常感谢你。 - arnie

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接