问题描述
是否可以在EntityTypeConfiguration中为TPH结构指定鉴别器列/值,而不是DbContext的OnModelCreating方法。
Is it possible to specify the discriminator column/value for a TPH structure in the EntityTypeConfiguration rather than the OnModelCreating method of the DbContext.
基本原理是我喜欢的所有关于类的持久性模型的信息都在一个地方而不是在多个位之间分开。
Rationale is that I like to have all info about a class's persistence model in one place rather than split between multiple bits.
推荐答案
是的,当您在OnModelCreating中调用modelBuilder.Entity< MyClass>()时,它只会返回一个EntityTypeConfiguration,因此它与API表面完全相同。
Yes, when you call modelBuilder.Entity<MyClass>() in OnModelCreating it just gives you back an EntityTypeConfiguration, so it's exactly the same API surface.
public class ProductConfig : EntityTypeConfiguration<Product>
{
public ProductConfig()
{
Map<Product>(m => m.Requires("Type").HasValue("Current"))
.Map<DiscontinuedProduct>(m => m.Requires("Type").HasValue("Old"));
}
}
~Rowan
这篇关于EntityTypeConfiguration中的Discriminator列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!