实体框架核心配置

Jaj*_*jan 8 entity-framework entity-framework-core asp.net-core

在EF 6中,我们可以直接将EntityTypeConfiguration传递给模型构建器来构建映射,并使我们的配置类与上下文分离,而不会在代码中过于冗长.

他们是否在EF核心中删除了这些地图.有没有办法在每个类的模型构建器中添加配置?

use*_*864 15

EntityFrameworkCore2.0有一个IEntityTypeConfiguration<TEntity>可以用作:

class ApplicationUserMap : IEntityTypeConfiguration<ApplicationUser>
{
  public void Configure(EntityTypeBuilder<Customer> builder)
  {
    builder.ToTable("user", "identity");

    builder.Property(p => p.Id)
        .HasColumnName("id");
     ...
   }
}

...
// OnModelCreating
modelBuilder.ApplyConfiguration(new ApplicationUserMap());
Run Code Online (Sandbox Code Playgroud)


Cri*_*jak 12

最好的方法是使配置代码远离OnModelCreating方法.所以你可以这样做:

创建一个用于存储实际配置的类:

public class ApplicationUserConfiguration
{
    public ApplicationUserConfiguration(EntityTypeBuilder<ApplicationUser> entity)
    {
        // Here you have all the good stuff
        entity.ToTable("user", "identity");

        entity.Property(p => p.Id)
            .HasColumnName("id);

        // And so on ....
    }
}
Run Code Online (Sandbox Code Playgroud)

并在您OnModelCreating实例化新创建的类并传递正确的实体:

protected override void OnModelCreating(ModelBuilder builder)
{
    base.OnModelCreating(builder);

    // Your custom configs here
    new ApplicationUserConfiguration(builder.Entity<ApplicationUser>());
}
Run Code Online (Sandbox Code Playgroud)

干净,是实现目标的简单方法.


Win*_*Win 8

有没有办法在每个类的模型构建器中添加配置?

如果数据库中有数百个表,那么肯定会有很多额外的工作,您必须手动配置每个映射类.

借助这种扩展方法,您可以在很少的代码行中获得所需的结果,例如优质的旧EF 6.

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    Type[] types = typeof(EntityTypeConfiguration<>).GetTypeInfo().Assembly.GetTypes();
    IEnumerable<Type> typesToRegister = types
        .Where(type => !string.IsNullOrEmpty(type.Namespace) &&
                        type.GetTypeInfo().BaseType != null &&
                        type.GetTypeInfo().BaseType.GetTypeInfo().IsGenericType &&
                        type.GetTypeInfo().BaseType.GetGenericTypeDefinition() == typeof(EntityTypeConfiguration<>));

    foreach (var type in typesToRegister)
    {
        dynamic configurationInstance = Activator.CreateInstance(type);
        ModelBuilderExtensions.AddConfiguration(modelBuilder, configurationInstance);
    }

    base.OnModelCreating(modelBuilder);
}
Run Code Online (Sandbox Code Playgroud)

扩展方法

public abstract class EntityTypeConfiguration<TEntity> where TEntity : class
{
    public abstract void Map(EntityTypeBuilder<TEntity> builder);
}

public static class ModelBuilderExtensions
{
    public static void AddConfiguration<TEntity>(ModelBuilder modelBuilder, EntityTypeConfiguration<TEntity> configuration)
        where TEntity : class
    {
        configuration.Map(modelBuilder.Entity<TEntity>());
    }
}
Run Code Online (Sandbox Code Playgroud)

映射类

public class UserMap : EntityTypeConfiguration<User>
{
    public override void Map(EntityTypeBuilder<User> builder)
    {
        // Primary Key
        builder.HasKey(t => t.Id);
        ...
    } 
}
Run Code Online (Sandbox Code Playgroud)