如何使用列模型覆盖 OnModelCreating(ModelBuilder modelBuilder) 方法?

How can I override OnModelCreating(ModelBuilder modelBuilder) method using a column model?

我有 column、columnA 和 columnB 模型。我正在尝试覆盖“protected override void OnModelCreating(ModelBuilder modelBuilder)”方法并尝试实现支持字段。

public class Column<TKey> : AuditableModel<TKey>
    where TKey: IComparable
{
    public string Name { get; set; }
    [Required]
    public Settings Settings {get;,set;} = new Settings();
}

public class ColumnA : Column<Guid>
{
    public Guid Id { get; set; }
}
public class ColumnB : Column<Guid>
{
    public Guid Id { get; set; }
}

protected override void OnModelCreating(ModelBuilder modelBuilder)
    { 
        modelBuilder.Entity<ColumnA>()
            .Property(x => x.Settings)

        modelBuilder.Entity<ColumnB>()
            .Property(x => x.Settings)

}

如何使用 "Column" 类型作为支持字段,而不是分别对 ColumnA 和 columnB 执行此操作(在 modelCreating 方法上)?

以下是我如何处理我的 EF 项目。这是一个 Car 模型的简单示例,请根据您的需要对其进行调整。

public class Car {
    public int Id { get; set; }
    public string Name { get; set; }
}

internal sealed class CarConfiguration :
    IEntityTypeConfiguration<Car> {
    public void Configure(
        EntityTypeBuilder<Car> builder) {
        builder.ToTable("Cars").HasKey(
            t => t.Id);

        builder.Property(
            p => p.Name).HasMaxLength(255).IsRequired();
    }
}

public class MyContext :
    DbContext {
    protected override void OnModelCreating(
        ModelBuilder modelBuilder) {
        var assembly = typeof(MyContext).Assembly;

        modelBuilder.ApplyConfigurationsFromAssembly(assembly);
    }
}

您可以阅读more on it here