如何从当前程序集自动添加所有实体类型配置<>?



如何从当前程序集自动添加所有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);

相关内容

  • 没有找到相关文章

最新更新