【问题标题】:Entity Framework To A Remote Server (Connection String)实体框架到远程服务器(连接字符串)
【发布时间】:2026-02-09 05:10:02
【问题描述】:

我在所有项目中都安装了 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>

【问题讨论】:

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

标签: c# sql-server entity-framework connection-string remote-server


【解决方案1】:

您需要创建或者确保您的连接字符串的name 是您的上下文的完全限定名称,或者为您的上下文创建一个显式的默认构造函数。由于您在 cmets 中提到了它,因此您提供的链接对您不起作用,因为您使用的是代码优先。请改用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>

【讨论】:

  • 如果您希望连接字符串与 EF 约定不同,则只需要覆盖构造函数。 EF 使用约定来查找连接字符串,因此它正在查找名为 .DivAppsDataContext 的连接字符串。它没有找到,所以它回退到 machine.config 中的默认 LocalSqlServer 字符串。
  • 我尝试了构造函数以及命名约定方式。我在这里缺少什么?
  • @ErikFunkenbusch 感谢您的提醒,我进行了编辑以说明这一点。 arnie,我发布了一个成功连接的测试控制台应用程序,试一试,看看它是否适合你。
  • @DrewJordan 它有效。你是对的,我有代码优先的方法。非常感谢。