entity-framework - 如何找到流利的API映射了哪个表?

标签 entity-framework ef-code-first fluent-interface

我需要找到映射到 EntityTypeConfiguration 类的表。
例如:

  public class PersonMap : EntityTypeConfiguration<Person>
    {
        public PersonMap()
        {
    ...
            this.ToTable("Persons");
    ....

        }
    }

我需要类似反向映射的东西:
var map=new PersonMap(); 
string table =map.GetMappedTableName();

我怎样才能做到这一点?

最佳答案

将字段添加到 PersonMap:

public class PersonMap : EntityTypeConfiguration<Person>
{
    public string TableName { get { return "Persons"; } }
    public PersonMap()
    {
        ...
        this.ToTable(TableName);
        ...
    }
}

像这样访问它:
var map = new PersonMap(); 
string table = map.TableName;

如果您可能不知道 map 的类型,请使用接口(interface):
public interface IMap
{
    string TableName { get; }
}
public class PersonMap : EntityTypeConfiguration<Person>, IMap
{
    public string TableName { get { return "Persons"; } }
    public PersonMap()
    {
        ...
        this.ToTable(TableName);
        ...
    }
}

像这样访问:
IMap map = new PersonMap(); 
string table = map.TableName;

关于entity-framework - 如何找到流利的API映射了哪个表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12564408/

相关文章:

java - 是否可以检测Fluent接口(interface)是否是第一次调用?

php - 闭包如何帮助创建 DSL/fluent 接口(interface) : PHP examples?

.NET/Entity Framework Core/SQL Server 过程返回重复值

c# - 如何首先使用带有 EF 代码的可重用查询/表达式来获取子实体作为父级中的 DTO?

c# - Entity Framework 6 深度复制/克隆具有动态深度的实体

c# - LINQ to Entities 不支持指定的类型成员 'Title'

.net - 在 Entity Framework Code First 中将 Guid 属性映射到 Oracle

c# - Entity Framework 6 Code First - 自定义类型映射

c# - Entity Framework 中的 "The number of properties in the Dependent and Principal Roles in a relationship constraint must be identical"问题

entity-framework - 使用 EF Code First 示例编写契约(Contract)和 Fluent API