如何自动从当前程序集中添加所有EntityTypeConfiguration ?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 }} (adsbygoogle = window.adsbygoogle || []).push({}); 最佳答案 这很容易,因为DbModelBuilder为此提供了特殊的方法。尝试在OnModelCreating方法中添加以下内容:modelBuilder.Configurations.AddFromAssembly(typeof(MyDbContext).Assembly); (adsbygoogle = window.adsbygoogle || []).push({});
10-06 07:10