有没有办法用DbModelBuilder定义表信息?
类似的东西:
entity.GetType().GetTableName()
最高
编辑:
id喜欢实现以下
public static class Helper
{
public string GetTableName(Type type) {
// ??
}
}
现在我想按类型
获取表名var type = someEntity.getType();
var sql = "delete from " + Helper.GetTableName(type) + " where id in (...)"
答案 0 :(得分:1)
我能想象的唯一解决方案就是反思。这是
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
// model mappings
base.OnModelCreating(modelBuilder);
// table mapping
var config = modelBuilder.Configurations
.GetPrivateFieldValue("_modelConfiguration")
.GetPrivateFieldValue("ActiveEntityConfigurations");
var mapping = new Hashtable();
foreach (var c in (IEnumerable)config)
{
var type = (Type)c.GetPrivateFieldValue("ClrType");
var tableName = (string)c.GetPrivateFieldValue("EntitySetName");
mapping[type] = tableName;
}
// store mapping whereever needed
}
主要思想是在base.OnModelCreating调用后获取配置对象。