【发布时间】: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