简体   繁体   English

实体框架6 T4获取EntitySet名称

[英]Entity Framework 6 T4 Get EntitySet Name

I am utilizing the T4 template in EF 6.1.3. 我正在使用EF 6.1.3中的T4模板。 Specifically I am creating some additional classes when generated. 具体来说,我在生成时会创建一些其他类。 Stubbing out some basic CRUD methods. 列出一些基本的CRUD方法。 I need to access the EntitySet name. 我需要访问EntitySet名称。 The T4 iterates over... T4遍历了...

var itemCollection = new EdmMetadataLoader(textTransform.Host, textTransform.Errors).CreateEdmItemCollection(inputFile);

Those objects are of type EntityType . 这些对象的类型为EntityType I need to find the associated EntitySet.Name property. 我需要找到关联的EntitySet.Name属性。

I am stumped and any help is appreciated. 我很沮丧,任何帮助表示赞赏。

After some digging through some other T4 templates I found a solution. 在研究了其他一些T4模板后,我找到了解决方案。 It appears that in previous versions the EF template would utilize the MetadataLoader class and now there is a custom EdmMetadataLoader class in the file itself. 似乎在以前的版本中,EF模板将使用MetadataLoader类,现在文件本身中有一个自定义的EdmMetadataLoader类。 This new class did not expose any access to the EntitySet itself. 这个新类没有公开对EntitySet本身的任何访问。

After getting the Entity collection via... 通过获取实体集合后...

var itemCollection = new EdmMetadataLoader(textTransform.Host, textTransform.Errors).CreateEdmItemCollection(inputFile);

You can get access to the container... 您可以访问该容器...

var container = itemCollection.OfType<EntityContainer>().FirstOrDefault();

Then in the iteration over the entities... 然后在实体的迭代中...

    foreach (var entity in typeMapper.GetItemsToGenerate<EntityType>(itemCollection))
    {
        fileManager.StartNewFile(entity.Name + ".cs");
        var entitySet = container.BaseEntitySets.OfType<EntitySet>().FirstOrDefault(set => set.ElementType == entity);
        ...
    }

Not 100% sure if this is the proper approach, but it does the job and I get the EntitySet name in following property... 不能100%确定这是否是正确的方法,但是可以完成工作,并且我在以下属性中获得EntitySet名称...

entitySet.Name

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM