Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#POCO T4模板,生成接口?_C#_Entity Framework_Poco - Fatal编程技术网

C#POCO T4模板,生成接口?

C#POCO T4模板,生成接口?,c#,entity-framework,poco,C#,Entity Framework,Poco,有人知道POCO T4模板的任何经过调整的版本,它可以生成接口和类吗? i、 如果我在.edmx文件中有电影和演员实体,我需要获得以下类和接口 interface IMovie { string MovieName { get; set; } ICollection<IActor> Actors { get; set; } //instead of ICollection<Actor> } class Movie : IMovie { strin

有人知道POCO T4模板的任何经过调整的版本,它可以生成接口和类吗? i、 如果我在.edmx文件中有电影和演员实体,我需要获得以下类和接口

interface IMovie
{
    string MovieName { get; set; }
    ICollection<IActor> Actors { get; set; } //instead of ICollection<Actor>
}

class Movie : IMovie
{
    string MovieName { get; set; }
    ICollection<IActor> Actors { get; set; } //instead of ICollection<Actor>
}

interface IActor
{
    string ActorName { get; set; }
}

class Actor
{
    string ActorName { get; set; }
}
接口IMovie
{
字符串MovieName{get;set;}
ICollection Actors{get;set;}//而不是ICollection
}
班级电影:IMovie
{
字符串MovieName{get;set;}
ICollection Actors{get;set;}//而不是ICollection
}
接口振荡器
{
字符串ActorName{get;set;}
}
班级演员
{
字符串ActorName{get;set;}
}

此外,为了防止我编写自己的实体,POCO代理(我需要它们用于延迟加载)是否可以使用如上所示的接口声明?

您可以编辑生成POCO实体的默认T4模板来生成接口。不久前我在一个工作项目上做了这个。涵盖了我们如何做到这一点的jist。这相对容易

下面是T4模板的一个片段,可能会有所帮助。我们将此代码插入到生成POCO实体的默认T4模板中

<#
    GenerationEnvironment.Clear();
    string templateDirectory = Path.GetDirectoryName(Host.TemplateFile);    
    string outputPath = Path.Combine(templateDirectory + @"..\..\Models\Interfaces\Repositories\IEntitiesContext.cs");
#>

using System;
using System.Data.Objects;
using Models.DataModels;

namespace Interfaces.Repositories
{
    #pragma warning disable 1591
    public interface IEntitiesContext : IDisposable
    {
    <#
        region.Begin("ObjectSet Properties", 2);

        foreach (EntitySet entitySet in container.BaseEntitySets.OfType<EntitySet>())
        {
#>
        IObjectSet<<#=code.Escape(entitySet.ElementType)#>> <#=code.Escape(entitySet)#> { get; }
<#
        }
        region.End();

        region.Begin("Function Imports", 2);

        foreach (EdmFunction edmFunction in container.FunctionImports)
        {
            var parameters = FunctionImportParameter.Create(edmFunction.Parameters, code, ef);
            string paramList = String.Join(", ", parameters.Select(p => p.FunctionParameterType + " " + p.FunctionParameterName).ToArray());
            if (edmFunction.ReturnParameter == null)
            {
                continue;
            }
            string returnTypeElement = code.Escape(ef.GetElementType(edmFunction.ReturnParameter.TypeUsage));

#>
    ObjectResult<<#=returnTypeElement#>> <#=code.Escape(edmFunction)#>(<#=paramList#>);
<#
        }

        region.End();
#>

        int SaveChanges();
        ObjectContextOptions ContextOptions { get; }
        System.Data.Common.DbConnection Connection { get; }
        ObjectSet<T> CreateObjectSet<T>() where T : class;
    }
    #pragma warning restore 1591
}
<#
        System.IO.File.WriteAllText(outputPath, GenerationEnvironment.ToString());
        GenerationEnvironment.Clear();
#>

使用制度;
使用System.Data.Object;
使用模型。数据模型;
名称空间接口.存储库
{
#pragma警告禁用1591
公共界面IEntiesContext:IDisposable
{
IObjectSet{get;}
p、 FunctionParameterType+“”+p.FunctionParameterName).ToArray();
if(edmFunction.ReturnParameter==null)
{
继续;
}
字符串returnTypeElement=code.Escape(ef.GetElementType(edmFunction.ReturnParameter.TypeUsage));
#>
ObjectResult();
int SaveChanges();
ObjectContextOptions上下文选项{get;}
System.Data.Common.DbConnection连接{get;}
ObjectSet CreateObjectSet(),其中T:class;
}
#pragma警告恢复1591
}
有人知道POCO T4模板的任何经过调整的版本,它可以生成接口和类吗

对于生成接口,Microsoft没有提供任何官方支持

这里有一个演示如何强制实体框架实现接口的演练。


或者,您可以下载自定义T4文件的工作示例,以生成接口和类:


Jonna,我很想知道您的方法是否成功,同时调整下面答案中的代码以生成POCO+接口。我们正在研究以同样的方式使用接口,但还没有找到解决方法。最好的方面是,TimoThis T4将为上下文生成接口,但它似乎不会为实际实体(IMovie、iAtor等)生成接口,对吗?您可以下载自定义T4文件的工作示例,以生成接口以及类: