我正在尝试使用 Entity Framework 5 Code First 来实现状态模式。
我的场景有一个名为 LotStatus 的抽象部分基类和 5 个继承自此的类:OpenStatus、AwardedStatus、CancelledStatus、ContractSignedStatus 和 EvaluationStatus:
public abstract partial class LotStatus { ... }
public class OpenStatus : LotStatus { ... }
public class AwardedStatus : LotStatus { ... }
在 DBContext 的 OnModelCreating 中,我将配置添加到模型构建器以映射这些实体:
modelBuilder.Configurations.Add(new LotStatusConfiguration());
LotStatusConfiguration 的代码如下:
public class LotStatusConfiguration : EntityTypeConfiguration<LotStatus>
{
public LotStatusConfiguration()
{
ToTable("LotStatus");
HasKey(ls => ls.LotStatusID);
this.Map<OpenStatus>(pk => pk.Requires("Discriminator").HasValue(typeof(OpenStatus).AssemblyQualifiedName).HasColumnType("nvarchar(max)"));
this.Map<EvaluationStatus>(pk => pk.Requires("Discriminator").HasValue(typeof(EvaluationStatus).AssemblyQualifiedName).HasColumnType("nvarchar(max)"));
this.Map<AwardedStatus>(pk => pk.Requires("Discriminator").HasValue(typeof(AwardedStatus).AssemblyQualifiedName).HasColumnType("nvarchar(max)"));
this.Map<ContractSignedStatus>(pk => pk.Requires("Discriminator").HasValue(typeof(ContractSignedStatus).AssemblyQualifiedName).HasColumnType("nvarchar(max)"));
this.Map<CancelledStatus>(pk => pk.Requires("Discriminator").HasValue(typeof(CancelledStatus).AssemblyQualifiedName).HasColumnType("nvarchar(max)"));
}
}
当 EF 尝试创建数据库时,它会引发错误:
The type 'OpenStatus' was not mapped. Check that the type has not been explicitly excluded by using the Ignore method or NotMappedAttribute data annotation. Verify that the type was defined as a class, is not primitive, nested or generic, and does not inherit from EntityObject.
我没有任何 [NotMapped] 标签,所以我在做什么?有什么想法?
谢谢!