Skip to main content
 首页 » 编程设计

c#之使用 EntityFramework 源项目而不是其程序集的解决方案无法成功启动

2024年02月06日9cmt

我有一个 WinForm 项目,它使用 EntityFramework 6.1.2-beta 和 .NET4,它运行良好,直到我决定将 EF 程序集替换为其源代码,以实现某些跟踪目标,因此我从项目引用中删除了 EntityFramework.dll 和 EntityFramework.SqlServer.dll并从 CodePlex 获取它们的源代码并将它们添加到我的解决方案中。

我将这些新项目配置更改为 DebugNet40(因为我的项目使用 .NET 4,但使用 EntityFramework 和 EntityFramework。 SqlServer 项目与 .NET 4.5 一起工作。

它编译成功,但是现在当我想运行我的项目时,出现以下错误:

An unhandled exception of type 'System.IO.FileLoadException' occurred in Microsoft.VisualStudio.HostingProcess.Utilities.dll

Additional information: Could not load file or assembly 'EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' or one of its dependencies. Strong name validation failed. (Exception from HRESULT: 0x8013141A)

这是我的app.config 文件:

<?xml version="1.0" encoding="utf-8"?> 
<configuration> 
  <configSections> 
     <section name="entityFramework"  
              type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection,  
              EntityFramework, Version=6.0.0.0, Culture=neutral,  
              PublicKeyToken=b77a5c561934e089" requirePermission="false" /> 
  </configSections> 
  <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> 
  <connectionStrings> 
     <add name="ERPContext"  
                connectionString="Data Source=.;Initial Catalog=MyDb; 
                Persist Security Info=True;User ID=sa;password=*****"  
                providerName="System.Data.SqlClient" /> 
  </connectionStrings> 
</configuration> 

我也从 app.config 中删除了 PublicKeyToken=b77a5c561934e089",但问题仍然存在。

有什么想法吗?

请您参考如下方法:

Entity Framework 源代码配置为生成延迟签名的程序集(这意味着它们被标记为“将来应该使用官方 key 签名”)。官方说明在Entity Framework's GitHub page演示如何禁用强名称验证(运行 EnableSkipStrongNames 任务:build/t:EnableSkipStrongNames),这样您就可以使用延迟签名的程序集,而无需任何人发布他们的私钥。这对于开发系统来说已经足够了。

如果您打算发布依赖于您自定义构建的修改后的 Entity Framework DLL 的产品,您应该生成一个新 key ,并修改 EF 源代码以使用该新 key 。