【发布时间】:2018-06-04 11:06:16
【问题描述】:
我正在尝试为我的实体创建一个通用配置类,但我被卡住了。
我有一个名为 EntityBase 的抽象类:
public abstract class EntityBase
{
public int Id { get; set; }
public int TenantId { get; set; }
public DateTime CreatedOn { get; set; }
public DateTime UpdatedOn { get; set; }
}
还有许多其他从 EntityBase 继承的类,我必须在每个类中使用相同的代码配置 DateTime 属性。这样:
void EntityTypeConfiguration<MyEntity>.Configure(EntityTypeBuilder<MyEntity> builder)
{
builder.HasIndex(e => e.TenantId);
builder.Property(e => e.CreatedOn)
.ValueGeneratedOnAdd()
.HasDefaultValueSql("GETDATE()");
// Other specific configurations here
}
我希望能够调用类似:builder.ConfigureBase() 并避免代码重复。有什么想法吗?
【问题讨论】:
-
这纯粹是为了设置一个默认值还是这个场景更像是“在插入/编辑时自动设置这些值”?
-
只是将 TenantId 配置为索引并设置 CreatedOn 属性的默认值
标签: c# entity-framework-core ef-core-2.0