实体框架 - 从实体获取表名称
我正在使用 Entity Framework 4.1 和 Code First 方法。我能够获取实体的存储模型类型和列名称:
var items = context.ObjectContext.MetadataWorkspace.GetItems<EntityType>(DataSpace.SSpace);
foreach (var i in items)
{
Console.WriteLine("Table Name: {0}", i.Name);
Console.WriteLine("Keys:");
foreach (var key in i.KeyMembers)
Console.WriteLine("\t{0} ({1})", key.Name, key.TypeUsage.EdmType.FullName);
Console.WriteLine("Members:");
foreach (var member in i.Members)
Console.WriteLine("\t{0} ({1})", member.Name, member.TypeUsage.EdmType.FullName);
}
我需要的是获取实体映射到的真实表名称。有不同的方法来指定(通过使用 Fluent-API .ToTable()、DataAnnotation [TableAttribute])。
有什么通用的方法可以获取这些信息吗?
I'm using the Entity Framework 4.1 with Code First approach. I'm able to get the storage model types and column names of my entities:
var items = context.ObjectContext.MetadataWorkspace.GetItems<EntityType>(DataSpace.SSpace);
foreach (var i in items)
{
Console.WriteLine("Table Name: {0}", i.Name);
Console.WriteLine("Keys:");
foreach (var key in i.KeyMembers)
Console.WriteLine("\t{0} ({1})", key.Name, key.TypeUsage.EdmType.FullName);
Console.WriteLine("Members:");
foreach (var member in i.Members)
Console.WriteLine("\t{0} ({1})", member.Name, member.TypeUsage.EdmType.FullName);
}
What I need is to get the real table name the entity is mapped to. There are different ways to specify that (by using Fluent-API .ToTable(), DataAnnotation [TableAttribute]).
Is there any common way to achieve this information?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
EF 6.1,代码优先:
EF 6.1, code-first:
我发现获取表名称的最简单方法如下:
这将为您提供表实体。
然后,您可以执行以下操作来提取名称:
请注意,如果您对上下文进行复数化,则需要撤消该操作。
The Easiest way I have found to get table names is the following:
That will get you the table entities.
Then you can do the following to extract the name:
Note if your pluralizing the context you will need to undo that.
还有另一种方法可以获取 EDM 表名称
There is an other way you can get the EDM Table Name