EF4.1 예외 테이블 - 계층 구조 상속으로 데이터베이스 만들기
-
13-11-2019 - |
문제
나는 계층 구조 내보내기를 보여주는 매우 간단한 프로젝트를 만들었습니다.데이터베이스를 생성하려고 시도하는 단위 테스트에서는 구성에 따라 여러 가지 오류 중 하나를 가져옵니다.
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.
.
내가 이해하는 것에서 우리는 기본 유형의 discriminator column / property를 정의해서는 안되는 것으로 밝혀지지 않아야합니다.
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
{
}
. 해결책
Decriminator는 엔티티의 속성을 맵핑 할 수 없습니다.discriminator는 엔티티의 유형을 정의합니다.그 이유는 삭제됩니다 - Discriminator는 인스턴스 유형을 정의합니다.런타임에 판별 자치를 변경할 수 있으면 어떻게됩니까?.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));
. 제휴하지 않습니다 StackOverflow