Спасибо всем за ответы!Таким образом, кажется, что решение не было слишком ужасным:
Я установил класс BaseEntityConfiguration как абстрактный класс, который принимает тип сущности, который я хочу настроить, и реализую интерфейс IEntityTypeConfiguration и делаю метод Configure способнымбыть 'overidable'.
BaseConfiguration
public abstract class BaseEntityConfiguration<TEntityType> : IEntityTypeConfiguration<TEntityType>
where TEntityType : BaseEntity
{
public virtual void Configure(EntityTypeBuilder<TEntityType> builder)
{
builder.HasKey(entity => entity.Guid);
// The created timestamp has a default value of the current system time for when the entity
// was created in the database. This value cannot be changed after it is set
builder.Property(entity => entity.CreatedAtTime)
.HasColumnType("datetime")
.HasValueGenerator(typeof(CurrentDateTimeGenerator))
.ValueGeneratedOnAdd();
// The updated timestamp has a default value of the minimum date time value and will only
// generate a new date time when the entity has been updated
builder.Property(entity => entity.UpdatedAtTime)
.HasColumnType("datetime")
.HasDefaultValue(DateTime.MinValue)
.HasValueGenerator(typeof(CurrentDateTimeGenerator))
.ValueGeneratedOnUpdate();
}
}
Затем в классах конфигурации объекта я расширяю этот класс BaseEntityConfiguration и переопределяю метод Configure, одновременно выполняя базовый метод Configure из абстрактного класса:
public class MilitaryUnitConfiguration : BaseEntityConfiguration<MilitaryUnit>
{
public override void Configure(EntityTypeBuilder<MilitaryUnit> builder)
{
base.Configure(builder);
// The unit name can only be 50 characters long and is unique
builder.Property(entity => entity.Name)
.HasColumnType("varchar(50)")
.HasMaxLength(50)
.IsRequired();
builder.HasAlternateKey(entity => entity.Name);
// The unit has a description that can be up to 100 character long
builder.Property(entity => entity.Description)
.HasColumnType("varchar(100)")
.HasMaxLength(100);
// The unit has multiple members
builder.HasMany<MilitaryMember>(entity => entity.Members);
// The unit has multiple sections
builder.HasMany<MilitaryUnitSection>(entity => entity.Sections);
}
}
Хотя я не проверил это полностью, похоже, что миграция была успешно настроена:
migrationBuilder.CreateTable(
name: "MilitaryUnits",
columns: table => new
{
Guid = table.Column<Guid>(nullable: false),
CreatedAtTime = table.Column<DateTime>(type: "datetime", nullable: false),
UpdatedAtTime = table.Column<DateTime>(type: "datetime", nullable: false, defaultValue: new DateTime(1, 1, 1, 0, 0, 0, 0, DateTimeKind.Unspecified)),
Name = table.Column<string>(type: "varchar(50)", maxLength: 50, nullable: false),
Description = table.Column<string>(type: "varchar(100)", maxLength: 100, nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_MilitaryUnits", x => x.Guid);
table.UniqueConstraint("AK_MilitaryUnits_Name", x => x.Name);
});