EF4.1 Exception creating Database with table-per-hierarchy inheritance

断了今生、忘了曾经 提交于 2019-12-04 14:09:38

You cannot map discriminator as property in the entity. Discriminator defines type of the entity. The reason is clear - discriminator defines instanced type. What should happen if you would be able to change discriminator value at runtime? How should .NET change the type of instanced object?

Define entities as:

public class User
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public virtual int Id { get; set; }

    [Required]
    public virtual string Username { get; set; }
}

public class InActiveUser : User
{
    public virtual DateTime DeActivatedDate { get; set; }
}

public class ActiveUser : User
{ }

And this should work:

modelBuilder.Entity<User>()
            .Map<InActiveUser>(x => x.Requires("IsActive").HasValue(false))
            .Map<ActiveUser>(x => x.Requires("IsActive").HasValue(true));
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!