本文介绍了如何添加所有EntityTypeConfiguration<>从当前装配自动进行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从当前程序集中自动添加所有EntityTypeConfiguration<>?

How do I add all EntityTypeConfiguration<> from current assembly automatically?

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


推荐答案

code> DbModelBuilder 为此提供了特殊的方法。尝试将其添加到 OnModelCreating 方法中:

It should be very easy as DbModelBuilder offers special method for that. Try add this within OnModelCreating method:

modelBuilder.Configurations.AddFromAssembly(typeof(MyDbContext).Assembly);

这篇关于如何添加所有EntityTypeConfiguration&lt;&gt;从当前装配自动进行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 09:59