问题描述
在EF Core 2.0中,我们能够从IEntityTypeConfiguration
派生用于更清晰的Fluent API映射(源).
In EF Core 2.0, we have the ability to derive from IEntityTypeConfiguration
for cleaner Fluent API mappings (source).
如何扩展此模式以利用基础实体?在下面的示例中,我如何拥有一个BaseEntityConfiguration
来减少LanguageConfiguration
和MaintainerConfiguration
中的重复,仅修改BaseEntityConfiguration
中BaseEntity
中的属性?这样的BaseEntityConfiguration
会是什么样子?以及如何在OnModelCreating()
中使用它?请参见示例末尾的代码中的TODO.
How can I extend this pattern to utilize a base entity? In the example below, how can I have a BaseEntityConfiguration
to reduce duplication in LanguageConfiguration
and MaintainerConfiguration
, modifying properties that are in the BaseEntity
only in the BaseEntityConfiguration
? What would such a BaseEntityConfiguration
look like; and how would it be used, if at all, in OnModelCreating()
? See the TODOs in-code near the end of the example.
示例:
public abstract class BaseEntity
{
public long Id { get; set; }
public DateTime CreatedDateUtc { get; set; }
public DateTime? ModifiedDateUtc { get; set; }
}
public class Language : BaseEntity
{
public string Iso6392 { get; set; }
public string LocalName { get; set; }
public string Name { get; set; }
}
public class Maintainer : BaseEntity
{
public string Email { get; set; }
public string Name { get; set; }
}
public class FilterListsDbContext : DbContext
{
public FilterListsDbContext(DbContextOptions options) : base(options)
{
}
public DbSet<Language> Languages { get; set; }
public DbSet<Maintainer> Maintainers { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
//TODO: Possibly add something like BaseEntityConfiguration?
modelBuilder.ApplyConfiguration(new LanguageConfiguration());
modelBuilder.ApplyConfiguration(new MaintainerConfiguration());
}
}
public class LanguageConfiguration : IEntityTypeConfiguration<Language>
{
public void Configure(EntityTypeBuilder<Language> entityTypeBuilder)
{
//TODO: Move this to something like BaseEntityConfiguration?
entityTypeBuilder.Property(b => b.CreatedDateUtc).HasDefaultValueSql("CURRENT_TIMESTAMP");
}
}
public class MaintainerConfiguration : IEntityTypeConfiguration<Maintainer>
{
public void Configure(EntityTypeBuilder<Maintainer> entityTypeBuilder)
{
//TODO: Move this to something like BaseEntityConfiguration?
entityTypeBuilder.Property(b => b.CreatedDateUtc).HasDefaultValueSql("CURRENT_TIMESTAMP");
}
}
推荐答案
像这样的东西可以工作(未经测试)吗?
Something like this could work (untested)?
public abstract class BaseEntityTypeConfiguration<TBase> : IEntityTypeConfiguration<TBase>
where TBase : BaseEntity
{
public virtual void Configure(EntityTypeBuilder<TBase> entityTypeBuilder)
{
//Base Configuration
}
}
public class MaintainerConfiguration : BaseEntityTypeConfiguration<Maintainer>
{
public override void Configure(EntityTypeBuilder<Maintainer> entityTypeBuilder)
{
entityTypeBuilder.Property(b => b.CreatedDateUtc).HasDefaultValueSql("CURRENT_TIMESTAMP");
base.Configure(entityTypeBuilder);
}
}
这篇关于将IEntityTypeConfiguration与基础实体一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!