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

标签 inheritance entity-framework-4.1 mapping table-per-hierarchy

我创建了一个非常简单的项目来演示每个层次结构的表继承。在我尝试生成数据库的单元测试中,根据配置,我得到了很多错误之一:

没有 Required() 方法

Map<InActiveUser>(x => x.Requires("IsActive").HasValue(false));
Map<ActiveUser>(x => x.Requires("IsActive").HasValue(true));

交付:

System.Data.DataException : An exception occurred while initializing the database. See the InnerException for details.
  ----> System.Data.EntityCommandCompilationException : An error occurred while preparing the command definition. See the inner exception for details.
  ----> System.Data.MappingException : 
(6,10) : error 3032: Problem in mapping fragments starting at line 6:Condition member 'User.IsActive' with a condition other than 'IsNull=False' is mapped. Either remove the condition on User.IsActive or remove it from the mapping.

使用 Required() 方法:

Map<InActiveUser>(x => x.Requires("IsActive").HasValue(false).IsRequired());

Map<ActiveUser>(x => x.Requires("IsActive").HasValue(true).IsRequired());

交付:

System.Data.DataException : An exception occurred while initializing the database. See the InnerException for details.
  ----> System.Data.EntityCommandCompilationException : An error occurred while preparing the command definition. See the inner exception for details.
  ----> System.Data.MappingException : 
(6,10) : error 3023: Problem in mapping fragments starting at lines 6, 13, 19:Column User.IsActive has no default value and is not nullable. A column value is required to store entity data.

据我了解,我们不应该在基本类型上定义鉴别器列/属性,但无论哪种方式,无论是否定义列似乎都没有区别:

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

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

    [Required]
    [DefaultValue(true)]
    public bool IsActive { get; set; } //have tried without this property
  }

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

  public class ActiveUser : User
  {
  }

最佳答案

您不能将鉴别器映射为实体中的属性。鉴别器定义实体的类型。原因很明确——鉴别器定义了实例类型。如果您能够在运行时更改鉴别器值,会发生什么情况? .NET 应该如何改变实例化对象的类型?

将实体定义为:

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
{ }

这应该有效:

modelBuilder.Entity<User>()
            .Map<InActiveUser>(x => x.Requires("IsActive").HasValue(false))
            .Map<ActiveUser>(x => x.Requires("IsActive").HasValue(true));

关于inheritance - EF4.1 Exception creating Database with table-per-hierarchy 继承,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6001198/

相关文章:

python - 为什么防止实例化理论上抽象的类很重要?

c# - 使用代码优先的 Entity Framework 中的问题建模关系

many-to-many - 如何让 EF 4.1 RC 代码优先使用正确的外键?

NHibernate 3.2 多对多代码映射

c# - 如何将单个实体映射到多个表

java - 将异常从子类传递到父类(super class)

C#继承与虚函数混淆

c++ - 如何创建继承类对象的数组?

many-to-many - SaveChanges() Entity Framework 4.1 的问题

php - 匹配半径内的经度和纬度