Skip to main content
 首页 » 编程设计

c#之将程序集添加到 SQL Server 2012 时出错

2024年02月06日12虾米姐

如果有人能就我的问题提出建议,我将不胜感激。

我有一个 EF 4.5 版的类库项目。

项目的

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=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false"/> 
  </configSections> 
  <connectionStrings> 
    <add name="Entities" connectionString="..." providerName="System.Data.EntityClient"/> 
    <add name="REPORTSEntities" connectionString="..." providerName="System.Data.EntityClient"/> 
  </connectionStrings> 
  <entityFramework> 
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework"/> 
  </entityFramework> 
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/></startup></configuration> 

当我尝试将 dll 添加到 SQL Server 2012 时,出现错误:

Assembly 'entityframework, version=5.0.0.0, culture=neutral,publickeytoken=b77a5c561934e089.' was not found in the SQL catalog. (Error:6503)

我做错了什么?

非常感谢。

请您参考如下方法:

SQL Server 的 CLR 主机在设计上受到相当的限制。这是受支持的 .NET Framework 库的列表(找到 here )。请注意,这是一个相当短的列表, Entity Framework 不在其中。

  • 自定义编码器
  • Microsoft.VisualBasic
  • Microsoft.VisualC
  • mscorlib
  • 系统
  • 系统配置
  • 系统数据
  • 系统.Data.OracleClient
  • 系统.Data.SqlXml
  • 系统.部署
  • 系统.安全
  • 系统.交易
  • 系统.Web.服务
  • 系统.xml
  • System.Core.dll
  • System.Xml.Linq.dll

我无法想象为什么在数据库内部运行时需要加载数据访问框架。公平地说,如果您将它们全部加载为 UNSAFE,您可以执行此操作,假设它们都是纯 MSIL 并且没有混合。但是加载 _un_supported .NET Framework 库的几个风险之一是,如果它们随着时间的推移而改变(通过 Windows 更新或手动更新),那么您在 SQL Server 中运行的代码可能会停止工作。如果它只是一个更新版本,那么您需要手动更新您必须加载到 SQL Server 中的相关 .NET Framework DLL,以便首先让它工作。但是,如果其中一个 DLL 在更新中更改为混合,那么您将无法将更新版本加载到 SQL Server 中,并且必须更改您的代码以不再需要该 DLL。

但是,如果您加载它,然后在引用 EF DLL 的完整路径时执行 CREATE ASSEMBLY,它应该会自动引入所有依赖项。

综上所述,您确定需要将任何 DLL 加载到 SQL Server 中吗?没有理由为在 SQLCLR 中运行的任何内容提供 app.config 文件。