繁体   English   中英

如何在实体框架中找到属性映射到的表和列?

[英]How do I find the table and column a property maps to in the Entity Framework?

如何在运行时从ObjectContext.Metadata获取信息,以查找对象的属性映射到哪个表的哪个列?

编辑:

如果这仅适用于仅映射到一张表的实体,我感到很高兴。

这不使用元数据,但是这是我的方法:)

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.

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