我想为我的实体指定模式名称,而不指定表名。现在,我只能这样做:modelBuilder.Entity<T>().ToTable("MyEntities", "myschema");有没有一种方法可以这样做:modelBuilder.Entity<T>().ToTable("myschema")?请考虑到我不能使用PluralizationService并手动计算表名,因为PluralizationService变成了内部...
发布于 2013-02-11 22:48:58
不如..。
var t = typeof (T);
var name= t.Name;
modelBuilder.Entity<T>().ToTable(name, "myschema")如果需要上下文中的DbSet复数名称
public DbSet<Single> Plural{ get; set; }然后,可以修改这个小扩展以返回您想要的值。两者的组合,没有循环。但我相信你会找到合适的变种。
public static class BosDalExtensions
{
public static List<string> GetModelNames(this DbContext context ) {
var model = new List<string>();
var propList = context.GetType().GetProperties();
foreach (var propertyInfo in propList)
{
if (propertyInfo.PropertyType.GetTypeInfo().Name.StartsWith("DbSet"))
{
model.Add(propertyInfo.Name);
}
}
return model;
}
public static List<string> GetModelTypes(this DbContext context)
{
var model = new List<string>();
var propList = context.GetType().GetProperties();
foreach (var propertyInfo in propList)
{
if (propertyInfo.PropertyType.GetTypeInfo().Name.StartsWith("DbSet" ))
{
model.Add(propertyInfo.PropertyType.GenericTypeArguments[0].Name);
}
}
return model;
}
}https://stackoverflow.com/questions/14801354
复制相似问题