2011-08-04 4 views
2

이것은 내 (단순화 된) 문제입니다. 클래스 :Entity Framework 4.1의 파생 클래스에서 다른 데이터베이스 필드가없는 TPH

public class RetailExposure 
{ 
    public int RetailExposureId { get; set; } 
    public int RetailModelId { get; set; } 
} 


public class PocRetailExposure : RetailExposure 
{ 
    [NotMapped] 
    public string IncidentScore { get; set; } 
} 

내 OnModelCreating 코드 :

System.Data.MappingException: 
(6,10) : error 3032: Problem in mapping fragments starting at line 6:Condition 
member 'RetailExposure.RetailModelId' with a condition other than 'IsNull=False' 
is mapped. Either remove the condition on RetailExposure.RetailModelId or remove 
it from the mapping. 
:

 context.RetailExposures.Add(new RetailExposure { RetailModelId = 1 }); 
     context.SaveChanges(); 

나는 다음과 같은 오류가 발생합니다 : 그러나

protected override void OnModelCreating(DbModelBuilder modelBuilder) 
    { 
     modelBuilder.Entity<RetailExposure>().ToTable("dbo.RetailExposure"); 
     modelBuilder.Entity<RetailExposure>().Map<PocRetailExposure>(p => p.Requires("RetailModelId").HasValue(1).IsRequired()); 
    } 

나는 새로운 노출 레코드를 추가 할 때

내가 시도한 것 RetailModelId 필드에서 결정된 파생 클래스가있는 'RetailExposure'기본 클래스가 있어야합니다. 파생 클래스에는 데이터베이스에있는 속성이 없습니다. 나는 RetailModelId를 discriminator로 사용하도록 EF를 구성하는 방법을 생각한 것 같았지만 변경 사항을 저장할 때 여전히이 오류가 발생합니다.

이 기능을 사용하려면 컨텍스트를 어떻게 구성해야합니까? 아니면 EF가 현재 지원하지 않는 것을하려고합니까?

EF가 파생 클래스를 완전히 무시한다고 말할 수는 있지만 실제로 원하는 것은 아닙니다.

답변

3

구분 기호는 엔터티의 속성으로 매핑하면 안되며 지원되지 않습니다. Discriminator는 데이터베이스의 열에 불과하며 실제 엔터티의 하위 유형에 매핑됩니다. 판별자를 속성으로 사용하려면 TPH를 매핑 할 수 없습니다. 또한 부모 엔티티가 추상이 아닌 경우 판별 자에 대해 정의 된 값을 가져야합니다. 같은

뭔가 :

public class RetailExposure 
{ 
    public int RetailExposureId { get; set; } 
} 

public class PocRetailExposure : RetailExposure 
{ 
    [NotMapped] 
    public string IncidentScore { get; set; } 
} 

protected override void OnModelCreating(DbModelBuilder modelBuilder) 
{ 
    modelBuilder.Entity<RetailExposure>().ToTable("dbo.RetailExposure"); 
    modelBuilder.Entity<RetailExposure>() 
       .Map<RetailExposure>(p => p.Requires("RetailModelId").HasValue(0)) 
       .Map<PocRetailExposure>(p => p.Requires("RetailModelId").HasValue(1)); 
} 
+2

아. 물론 discriminator를 클래스의 속성에 매핑 할 수는 없습니다. 그렇게 할 수 있다면 매우 OO가 아닙니다! 그것을 고치고 RetailExposure 추상을 만들었습니다. 시험 합격. 예! 감사 –

관련 문제