如何从当前程序集自动添加所有entitytypconfiguration<> ?
public class Entities : DbContext
{
public Entities()
: base("Entities")
{
}
public virtual DbSet<User> Users { get; set; }
// ...
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
foreach(var configuration in this.GetAllConfigurations())
{
modelBuilder.Configurations.Add(configuration);
}
}
private ... GetAllConfigurations()
{
// TODO: Get all configurations from current ASSEMBLY
}
}
这应该很容易,因为DbModelBuilder
提供了特殊的方法。尝试将此添加到OnModelCreating
方法中:
modelBuilder.Configurations.AddFromAssembly(typeof(MyDbContext).Assembly);
简单回答
modelBuilder.Configurations.AddFromAssembly(GetType().Assembly);