2013-02-10 509 views
2

我想为我的实体分配模式名称,而不指定表名称。现在,我只能做:modelBuilder.Entity<T>().ToTable("MyEntities", "myschema");有没有办法做到这样的事情:modelBuilder.Entity<T>().ToTable("myschema")?请考虑,我不能用PluralizationService和手工计算表名,因为PluralizationService成为内部...实体框架6.模式名称

+0

也许[通过架构分区多租户(新功能http://entityframework.codeplex.com/wikipage?title=Multi-tenant% 20Migrations)是适合你的东西吗? ToTable方法不支持它。 – 2013-02-11 08:06:00

回答

1

......怎么

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; 
} 
}