T4 - 实体框架错误:未找到方法:'System.Data.Entity.DbSet`1 [英] T4 - Entity Framework Error: Method not found: 'System.Data.Entity.DbSet`1

查看:1388
本文介绍了T4 - 实体框架错误:未找到方法:'System.Data.Entity.DbSet`1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

试图创建一个我试图加载我可以使用一些以后值一个相当简单的T4:

Trying to create a fairly simple T4 which I'm trying to load some values that I can use for later:

<#@ template debug="false" hostspecific="false" language="C#" #>
<#@ assembly name="System.Core" #>
<#@ assembly name="c:\users\<username>\documents\visual studio 2015\Projects\2_DataModelGenerator\packages\EntityFramework.6.1.3\lib\net45\EntityFramework.dll" #>
<#@ assembly name="c:\users\<username>\documents\visual studio 2015\Projects\2_DataModelGenerator\packages\EntityFramework.6.1.3\lib\net45\EntityFramework.SqlServer.dll" #>
<#@ assembly name="c:\users\<username>\documents\visual studio 2015\Projects\2_DataModelGenerator\2_Data\bin\Debug\2_Data.dll" #>

<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Data" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ import namespace="2_Data" #>

<#@ output extension=".cs" #>

<#
    DevEntities dbContext = new DevEntities();

            var labelClassNames = (from sd in dbContext.tblDatas
                                   where sd.SID == 155
                                   select new
                                   {
                                       SID = sd.SID.ToString(),
                                       SValue = sd.SValue.ToString()
                                   }).ToList();

#>

当我保存T4我得到这个错误:

When I save the T4 I get this error:

错误运行转换:system.missingMethodException而:方法
未找到:'System.Data.Entity.DbSet`1< 2_Data.tblData>
2_Data.DevEntities。 get_tblDatas()

Error Running transformation: System.MissingMethodException: Method not found: 'System.Data.Entity.DbSet`1<2_Data.tblData> 2_Data.DevEntities.get_tblDatas()'

我在另一种解决方案测试此代码(使用EF 6.1.3)和它的作品没有问题(加载数据到 VAR labelClassNames )。似乎只有在T4模板的问题。

I have tested this code in another solution (using EF 6.1.3) and it works without issue (loads data into var labelClassNames). Seems to be an issue only in T4 template.

我是否需要导入另一个命名空间? !我不知道我错过了什么。

Do I need to import another namespace? I'm not sure what I'm missing!

更新:

在我的TT代码:

        dbc context = new dbc();
        DevEntities mdc = new DevEntities();
        mdc = context.returnContext();

在我的类库中添加一个类:

Added a class in my class library:

public class dbc
{
    private DevEntities dbContext;
    public dbc()
    {
        dbContext = new DevEntities();
    }
    public DevEntities returnContext()
    {
        return dbContext;
    }
}



同样的错误之前:

Same error as before:

运行转换:system.missingMethodException而:方法不是
发现:System.Data.Entity.DbSet

Running transformation: System.MissingMethodException: Method not found: 'System.Data.Entity.DbSet

然而,当我调试T4模板,我得到不同的信息:

However, when I debug the t4 template I get a different message:

没有连接名为DevEntities'字符串可能会在
应用程序配置文件中找到。

No connection string named 'DevEntities' could be found in the application config file.

不过,我抄的App.config从我的类库文件,该文件包含.edmx文件到我的项目包含.TT文件

However, I have copied the App.config file from my class library that include the .edmx file down to my project that contains the .tt file

任何想法?

推荐答案

在T4模板的同一个项目中的类是不可见并简化其调试,下面是一步步在的EntityFramework没有问题成功运行T4和调试的便利性:

The classes in the same project of T4 template is not visible by it, and to simplify its debugging, the following is a step by step to run T4 successfully in EntityFramework without problem and ease of debugging:

第1步:创建模型一个单独的类库项目

为模型创建一个单独的类库项目,为您的应用程序和T4模板引用。

Create a separate class library project for the model, to be referenced by your application and T4 template.

这也简化了单元测试,并解决与T4诸多问题

That also simplify unit test and resolve many problems with T4.

第二步:解决连接字符串

要避免配置文件的问题而正确配置的connectionString,创造,扩展了Context类,以避免覆盖时重新生成上下文的分部类。
定义新的重载构造函数。

To avoid the problems of configuration file and configure the connectionString correctly, create a partial class which extend the Context class to avoid overwriting when re-generate the context. Define new overload constructor.

例如,NorthwindEntities

for example, NorthwindEntities

      using System.Data.Entity;
     namespace NorthWin
     {        
        public partial class NorthwindEntities : DbContext
        {
            public NorthwindEntities(string connString)
                : base(connString)
            {
            }
        }
    }

第三步:创建在单独的类(S)所有DAL方法

您在T4模板需要,在不同的类(ES)在同一个类库创建它的所有方法项目并从T4模板调用它们。

All methods that you need in T4 template,create it in separate class(es) in the same class library project and call them from T4 template.

在你的类明确定义的connectionString(注意连接字符串中的单引号)。
修改或可以使用包括模板。
例如:

Define the connectionString explicitly in your class (note the single quote in the connection string). or you can use the include template. example:

    using System.Linq;
    namespace NorthWin
    {
        public class DAL
        {

            string ConnectionString = @"metadata=res://*/NorthWind.csdl|res://*/NorthWind.ssdl|res://*/NorthWind.msl;provider=System.Data.SqlClient;provider connection string='data source=myserver;initial catalog=Northwind;persist security info=True;user id=xxx;password=yyy;MultipleActiveResultSets=True;App=EntityFramework';";

   public DAL (string connString)
    {
      ConnectionString =connString;
    }       
    public int GetCustomerCount()
            {
                var n = 0;
            // call ye new overload constructor
                using (var ctx = new NorthwindEntities(ConnectionString))
                {
                    n = ctx.Customers.Count();
                }
                return n;
            }
        }
    }



第4步:建立自己的T4
定义的EntityFramework和呼叫DAL方法

step 4: build your T4 Define the minimum assemblies for EntityFramework and Call DAL methods

例如

    <#@ template debug="false" hostspecific="true" language="C#" #>
    <#@ output extension=".txt" #>

      <#@ assembly name="System.Xml"#>
    <#@ assembly name="$(TargetDir)NorthWin.dll" #>
    <#@ assembly name="$(TargetDir)EntityFramework.dll" #>
    <#@ assembly name="$(TargetDir)EntityFramework.SqlServer.dll" #>
    <#@ assembly name="EnvDTE" #>
    <#@ assembly name="System.Configuration" #>

    <#@ import namespace="System.Configuration" #>
    <#@ import namespace="System" #>
     <#@ import namespace="System.Linq" #>
    <#@ import namespace="System.Collections.Generic" #>
     <#@ import namespace="NorthWin" #>
    <#

       int n=0;
        var dal = new DAL();
       / /call DAL methods
        n= dal.GetCustomerCount();

     #> 

    <#= n #>
    {
      ... // More code here. 
    }



T4模板的输出:

114
{
  ... // More code here. 
}



编辑:

您可以使用包含文件中所述获得配置文件中的connectionString:

You can get the connectionString from configuration file using the include file as described in:

注入你的web.config连接字符串转换成你的T4模板

然后你通过ConnectionString,以使用构造接受连接字符串,您的DAL类。

then you pass the connectionString to your DAL classes with constructor that accept connection String.

在模板中添加此代码

<#@ include file="ConfigurationAccessor.ttinclude" #>
<# 
 var config = new ConfigurationAccessor((IServiceProvider)this.Host,  @"path\to\ProjectWithConfig.csproj");
 string connectionString =   config.ConnectionStrings["MainConnectionString"].ConnectionString;
#>

这篇关于T4 - 实体框架错误:未找到方法:'System.Data.Entity.DbSet`1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆