[英]How do I find the table and column a property maps to in the Entity Framework?
How do I get the information from ObjectContext.Metadata
at runtime to find what column in what table a property on an object is mapped to? 如何在运行时从ObjectContext.Metadata
获取信息,以查找对象的属性映射到哪个表的哪个列?
EDIT: 编辑:
I'm happy if this only works for entities that map to one and only one table. 如果这仅适用于仅映射到一张表的实体,我感到很高兴。
This doesn't use MetaData, but here's how I do it :) 这不使用元数据,但是这是我的方法:)
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
public static string GetFullTableName(this Type t)
{
string exp = "[{0}].[{1}]";
if (Attribute.IsDefined(t, typeof(TableAttribute)))
{
var attr = (TableAttribute)t.GetCustomAttributes(typeof(TableAttribute), false).First();
return string.Format(exp, attr.Schema, attr.Name);
}
else
{
return string.Format("[dbo].[{0}]", t.Name);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.