从EF中的EntityType获取EntitySet名称 [英] Get the EntitySet name from an EntityType in EF

查看:391
本文介绍了从EF中的EntityType获取EntitySet名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定一个EntityType,例如Contact,如何从其中派生它将属于的EntitySet的名称,即诸如Contacts的复数形式?

解决方案

如果您已经拥有附属实体(显然您不需要第一行,只需使用现有实体):

 联系c = context.Contacts.Where(x => x.blah).FirstOrDefault(); 
string setName = c.EntityKey.EntitySetName;

或者如果没有:

  string className = typeof(Contact).Name 
var container =
context.MetadataWorkspace.GetEntityContainer(context.DefaultContainerName,DataSpace.CSpace);
string setName =(from container in container.BaseEntitySets
where meta.ElementType.Name == className
select meta.Name).First();


Given an EntityType, such as "Contact", how can I derive from it the name of the EntitySet it would belong to, i.e. the pluralization such as "Contacts"?

解决方案

If you already have an attached entity (obviously you don't need the first line, just use your existing entity):

  Contact c = context.Contacts.Where(x => x.blah).FirstOrDefault();
  string setName = c.EntityKey.EntitySetName;

Or if you don't:

 string className = typeof(Contact).Name
 var container =   
    context.MetadataWorkspace.GetEntityContainer(context.DefaultContainerName, DataSpace.CSpace);
    string setName = (from meta in container.BaseEntitySets
                                          where meta.ElementType.Name == className
                                          select meta.Name).First();

这篇关于从EF中的EntityType获取EntitySet名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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