Entity framework 使用T4获取实体映射表名

Entity framework 使用T4获取实体映射表名,entity-framework,t4,Entity Framework,T4,我正在尝试使用T4生成以下代码: public virtual IList<Frequency> GetAll() { using (var wc = new WCDataClassesDataContext()) { return wc.Frequencies.ToList(); } } public virtual IList<Frequency> GetAll(Expr

我正在尝试使用T4生成以下代码:

public virtual IList<Frequency> GetAll()
    {
        using (var wc = new WCDataClassesDataContext())
        {
            return wc.Frequencies.ToList();
        }
    }

    public virtual IList<Frequency> GetAll(Expression<Func<Frequency, bool>> whereClause)
    {
        using (var wc = new WCDataClassesDataContext())
        {
            return wc.Frequencies.Where(whereClause).ToList();
        }
    }
public虚拟IList GetAll()
{
使用(var wc=new WCDataClassesDataContext())
{
返回wc.Frequencies.ToList();
}
}
公共虚拟IList GetAll(表达式whereClause)
{
使用(var wc=new WCDataClassesDataContext())
{
返回wc.Frequencies.Where(whereClause.ToList();
}
}
在T4中,我使用的是:

foreach (var entity in typeMapper.GetItemsToGenerate<EntityType>(itemCollection))
{
    fileManager.StartNewFile(entity.Name + "BaseFinder.cs");
    BeginNamespace(code);
 #>

<#=codeStringGenerator.UsingDirectives(inHeader: false)#>
using System.Linq;
using System.Linq.Expressions;
<#=codeStringGenerator.EntityClassOpening(entity)#>
{

    public virtual IList<<#=entity.Name #>> GetAll()
    {
        using (var wc = new WCDataClassesDataContext())
        {
            return wc.[[Frequencies]].ToList();
        }
    }

    public virtual IList<<#=entity.Name #>> GetAll(Expression<Func<<#=entity.Name #>, bool>> whereClause)
    {
        using (var wc = new WCDataClassesDataContext())
        {
            return wc.[[Frequencies]].Where(whereClause).ToList();
        }
    }

......
}
foreach(typeMapper.GetItemsToGenerate(itemCollection)中的var实体)
{
fileManager.StartNewFile(entity.Name+“BaseFinder.cs”);
BeginNamespace(代码);
#>
使用System.Linq;
使用System.Linq.Expressions;
{
公共虚拟IList GetAll()
{
使用(var wc=new WCDataClassesDataContext())
{
返回wc.[Frequencies].ToList();
}
}
公共虚拟IList GetAll(表达式whereClause)
{
使用(var wc=new WCDataClassesDataContext())
{
返回wc.[Frequencies].Where(Where子句).ToList();
}
}
......
}
我想要实体映射到的主表名,而不是[[Frequencies]]。 我正在尝试设置各种可以在类中轻松使用的getter

你能告诉我解决这个问题的方法吗,或者有其他的方法吗

希望你明白我的意思


谢谢。

看起来您已经有了实体类型,所以我认为您已经接近了-您所需要做的就是获取具有多个关系的导航属性,您应该进行设置

比如:

EntityType et = entity.GetType();
foreach(var navProp in et.NavigationProperties.Where(np=>np.DeclaringType == et 
        && np.RelationshipMultiplicity == RelationshipMultiplicity.Many))
{
    string s = string.Format("public virtual IList<{0}> GetAll() ...", 
                           navProp.ToEndMember.GetEntityType().Name);

}
EntityType et=entity.GetType();
在et.NavigationProperties.Where(np=>np.DeclaringType==et
&&np.RelationshipMultiplicity==RelationshipMultiplicity.Many)
{
string s=string.Format(“公共虚拟IList GetAll()…”,
navProp.ToEndMember.GetEntityType().Name);
}

Entity Framework DB first generator已经做了类似的工作;如果你在EDMX下翻找,你应该会看到
*.Context.tt
或类似的东西。在那里,搜索
导航属性
,有一个代码字符串帮助器方法来生成类似的东西。

是的,我想这会有帮助,会有一个看看这个。有什么资源可以让我找到T4中使用的所有帮助器方法或类描述/类文档吗?我没有找到,但这并不意味着一个不存在。我很幸运地找到了VS在添加EDMX和EF实用工具后添加的
*.Context.tt
文件t包含,通常位于
C:\Program Files(x86)\Microsoft Visual Studio 10.0\Common7\IDE\Extensions\Microsoft\Entity Framework Tools\Templates\包含
。此外,您还可以在非T4上下文中使用
MetadataWorkspace
执行此操作。这个问题可能会有所帮助。