简体   繁体   English

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

[英]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.

相关问题 如何在Entity Framework中为同一表配置导航属性? - How do I configure a navigation property to the same table in Entity Framework? 如何使用 Entity Framework 和 .NET 5.0 构建查询以在相关表中查找不匹配的实体? - How do I contruct a query to find unmatched entities in related table with Entity Framework and .NET 5.0? 使用实体框架,如何在导航属性上指定排序? - Using Entity Framework, how do I specify a sort on a navagation property? 如何使用实体框架中的Enum替换Int属性? - How do I replace an Int property with an Enum in Entity Framework? 如何使用实体框架更新嵌套属性 - How do I update a nested property with Entity Framework 在实体框架中,如何在代码中创建关联属性? - In Entity Framework, how do I create an Association Property in code? 如何在实体框架中映射类型为“字典”的属性? - How do I map property with of type 'Dictionary' with Entity Framework? 我如何模拟实体框架的导航财产情报? - How Do I Mock Entity Framework's Navigational Property Intelligence? 如何使用实体框架将项目添加到列表属性 - How do i add items to a list property using Entity Framework 如何使用LINQ和实体框架6进行表联接? - How do I do a table join using LINQ and entity framework 6?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM