gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 08:29:23 25 4
gpt4 key购买 nike

我需要找到映射到 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com