简体   繁体   English

EF核心-实体映射配置

[英]EF Core - Entity Mapping Configuration

In previous versions of EF, you could configure all of your entity maps on the DbContext like this :- 在早期版本的EF中,您可以像这样在DbContext上配置所有实体映射:-

  protected override void OnModelCreating(DbModelBuilder modelBuilder)
            {
                modelBuilder.Configurations.AddFromAssembly(typeof(MyDbContext).Assembly);

                base.OnModelCreating(modelBuilder);
            }

However in the latest EF Core it seems you have to add each mapping individually like this :- 但是,在最新的EF Core中,您似乎必须像这样单独添加每个映射:-

protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.ApplyConfiguration(new UserMap());
            modelBuilder.ApplyConfiguration(new AddressMap());

            base.OnModelCreating(modelBuilder);
        }

Is there no similar way of add Entity Maps, as this is tedious. 没有类似的添加实体贴图的方法,因为这很繁琐。

This do the same of the AddFromAssembly of EF6: 这样做与AddFromAssembly相同:

var configurations = typeof(MyDbContext).Assembly.GetTypes()
                .Where(t => t.BaseType != null && t.BaseType.IsGenericType && t.BaseType.GetGenericTypeDefinition() == typeof(System.Data.Entity.ModelConfiguration.EntityTypeConfiguration<>));

foreach (var config in configurations)
{
    dynamic instance = System.Activator.CreateInstance(config);
    modelBuilder.Configurations.Add(instance);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM