加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 如何在T4中实例化EF6上下文?

发布时间:2020-12-15 22:10:49 所属栏目:百科 来源:网络整理
导读:我想在T4模板中实例化一个新的EF6上下文,但是我收到以下错误: System.InvalidOperationException: The ‘Instance’ member of the Entity Framework provider type ‘System.Data.Entity.SqlServer.SqlProviderServices,EntityFramework.SqlServer,Version
我想在T4模板中实例化一个新的EF6上下文,但是我收到以下错误:

System.InvalidOperationException: The ‘Instance’ member of the Entity Framework provider type ‘System.Data.Entity.SqlServer.SqlProviderServices,EntityFramework.SqlServer,Version=6.0.0.0,Culture=neutral,PublicKeyToken=b77a5c561934e089’ did not return an object that inherits from ‘System.Data.Entity.Core.Common.DbProviderServices’. Entity Framework providers must inherit from this class and the ‘Instance’ member must return the singleton instance of the provider.

解决方法

问题是无法实例化实体框架上下文,因为无法读取连接字符串.

我做了以下以获取连接字符串:

var map = new ExeConfigurationFileMap();
map.ExeConfigFilename = this.Host.ResolvePath(@"....Web.config");

var config = ConfigurationManager.OpenMappedExeConfiguration(map,ConfigurationUserLevel.None);
var connectionString = config.ConnectionStrings.ConnectionStrings["BrainPerformEntities"].ConnectionString;

但请确保您已导入以下内容以使上述代码正常工作:

<#@ import namespace="Microsoft.VisualStudio.TextTemplating" #>
<#@ import namespace="System.Configuration" #>
<#@ import namespace="System" #>

还要确保已引用所有实体框架程序集:

<#@ assembly name="$(MSBuildProjectDirectory)binEntityFramework.dll" #>
<#@ assembly name="$(MSBuildProjectDirectory)binEntityFramework.SqlServer.dll" #>

<#@ assembly name="System.Core.dll" #>
<#@ assembly name="System.Configuration" #>
<#@ assembly name="System.Data.dll" #>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读