【问题标题】:EntityFramework.SqlServer.dll not is getting added to the published folder only when I publish in RELEASE mode只有当我在 RELEASE 模式下发布时,EntityFramework.SqlServer.dll 才会被添加到已发布的文件夹中
【发布时间】:2014-10-15 11:57:08
【问题描述】:

我知道 EF6 EntityFramework.SqlServer 存在问题,并将 var type = typeof(System.Data.Entity.SqlServer.SqlProviderServices); 包含在上下文构造函数中。当我在 DEBUG 模式下发布时它工作正常。

仅当我在 RELEASE 模式下发布时才会出现以下错误。原因是已发布文件夹中缺少EntityFramework.SqlServer.dll。但是,bin 文件夹有 EntityFramework.SqlServer.dll 用于调试和发布模式。

错误:

实体框架提供程序类型 'System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer' 在应用程序配置文件中注册 对于具有不变名称“System.Data.SqlClient”的 ADO.NET 提供程序 无法加载。确保程序集限定名称是 并且该程序集可用于正在运行的应用程序。

为什么只有在我使用 RELEASE 模式发布时才会丢失?

【问题讨论】:

  • 在解决方案资源管理器中,选择对 EntityFramework.SqlServer.dll 的引用,并确保在发布配置中将其标记为“复制到输出”并将其与调试进行比较。
  • 您确定,调试和发布的数据库结构相同吗?
  • @libik:是的,两者具有相同的结构。此外,验证了调试和发布 bin 文件夹。它有 EntityFramework.SqlServer.dll。
  • @Dai:你的意思是复制本地吗?因为我的 EntityFramework.SqlServer.dll 属性中没有“复制到输出”属性。
  • 向您的程序集添加属性以引用 EntityFramework.SqlServer。详情见stackoverflow.com/a/48318806/1881344

标签: c# entity-framework-6


【解决方案1】:

不知道为什么;但是将此方法添加到您的上下文将使您的项目复制 dll

private void FixEfProviderServicesProblem()
        {
            // The Entity Framework provider type 'System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer'
            // for the 'System.Data.SqlClient' ADO.NET provider could not be loaded. 
            // Make sure the provider assembly is available to the running application. 
            // See http://go.microsoft.com/fwlink/?LinkId=260882 for more information.
            var instance = System.Data.Entity.SqlServer.SqlProviderServices.Instance;
        }

经过测试和工作 参考:Entity Framework Provider type could not be loaded?

【讨论】:

  • 这行得通.. 很可能是因为这个语句,entityframework.sqlserver 被“实际”使用了..
  • 确认修复了问题。
  • 你需要哪些程序集来引用这个?我尝试添加 System.Data 和 System.Data.Entity,但仍然无法编译。
【解决方案2】:

我也遇到了同样的问题。

发生的情况是这个EntityFramework.SqlServer.dll 没有被您的项目直接引用,而是在内部被EntityFramework.dll 引用。

由于我们没有在我们的代码中使用任何 EntityFramework.SqlServer.dll,因此 Roslyn 编译器应用了一些优化,知道它不需要这个组件,因此不会将其复制到@ 987654324@ 文件夹,对于Publish 也不会复制。

您需要做的是在代码中的任何位置创建对 EntityFramework.SqlServer.dll 的某个对象的引用。

例如,我有一个依赖注入容器配置类,我在其中配置我的存储库和上下文,我认为这将是一个不错的位置。所以我创建了一个我从不在任何位置使用的静态属性,但它解决了问题:

using System.Data.Entity.SqlServer;

public class ContainerConfiguration
{
    // HACK: This code snippet ensures that the DLL (EntityFramework.SqlServer.dll)
    // not removed by compiler optimizer (csc.exe roslyn)
    public static SqlProviderServices EntitySqlServerHack => SqlProviderServices.Instance;

    public void ContainerConfiguration(IContainer container)
    {
        InternalConfigure(container, false);
    }
}

【讨论】:

    【解决方案3】:

    听起来您的解决方案已损坏。我会重新制作解决方案并将所有文件复制到新的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-02-21
      • 1970-01-01
      • 2018-07-25
      • 1970-01-01
      • 1970-01-01
      • 2022-10-14
      • 1970-01-01
      • 2023-02-19
      相关资源
      最近更新 更多