【转】modelBuilder.Configurations.AddFromAssembly in EF Core
生活随笔
收集整理的這篇文章主要介紹了
【转】modelBuilder.Configurations.AddFromAssembly in EF Core
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
EntityFramework 6.x
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder); modelBuilder.Configurations.AddFromAssembly(typeof(MyDbContext).Assembly);
}
EF Core <= 2.1
public static class ModelBuilderExtensions
{
public static void ApplyAllConfigurations(this ModelBuilder modelBuilder)
{
var typesToRegister = Assembly.GetExecutingAssembly().GetTypes().Where(t => t.GetInterfaces()
.Any(gi => gi.IsGenericType && gi.GetGenericTypeDefinition() == typeof(IEntityTypeConfiguration<>))).ToList(); foreach (var type in typesToRegister)
{
dynamic configurationInstance = Activator.CreateInstance(type);
modelBuilder.ApplyConfiguration(configurationInstance);
}
}
} protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder); modelBuilder.ApplyAllConfigurations();
}
EF Core >= 2.2
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder); modelBuilder.ApplyConfigurationsFromAssembly(typeof(UserConfiguration).Assembly); // Here UseConfiguration is any IEntityTypeConfiguration
}
轉自:https://stackoverflow.com/questions/51842709/modelbuilder-configurations-addfromassembly-in-ef-core
總結
以上是生活随笔為你收集整理的【转】modelBuilder.Configurations.AddFromAssembly in EF Core的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java实验3 2020.4.6
- 下一篇: 庄导就业指导2020.4.6