EF4.1异常创建具有每层次表继承的数据库 [英] EF4.1 Exception creating Database with table-per-hierarchy inheritance

查看:164
本文介绍了EF4.1异常创建具有每层次表继承的数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个非常简单的项目来演示每个层次结构表的不一致性。在我试图生成数据库的单元测试中,我根据配置得到了一些错误:

I have created a very simple project to demonstrate table-per-hierarchy inhertiance. In my unit test which tries to generate the database, i get one of a number of errors depending on the config:

没有必需() method

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.

使用必需()方法:

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.

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

From what I understand we should not define the discriminator column/property on the base type, but either way it seems to make no difference with or without the column defined:

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


推荐答案

您无法将鉴别器映射为实体中的属性。 Discriminator定义实体的类型。原因很清楚 - 鉴别器定义了实例类型。如果您能够在运行时更改鉴别值,会发生什么? .NET应如何更改实例化对象的类型?

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?

将实体定义为:

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));

这篇关于EF4.1异常创建具有每层次表继承的数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆