2017-12-28 12 views
0

코드를 먼저 사용하여 데이터 프로젝트를 만들고 데이터 주석을 작성하려고하지만 아키텍처에 대한 의문점이 있습니다. 내 관계 스키마가 합리적인지, SQL에서 올바른지, EF에서 구현 가능한지 확실하지 않습니다. 하나의 자식 테이블에서 두 개의 상상력 테이블 중 하나로 조건부 관계를 만드는 방법. 내 ProcessRelation 테이블에서 두 관계를 저장합니다. JobBusinessArea - 견적 및 jobBussinesArea - PriceList. 나는 그 말을하기를 바란다. 도움과 제안을 주셔서 감사합니다.Entity Framework - 조건부 관계 - 하위 테이블 하나 또는 다른 상상력 표

[Table("ProcessRelation")] 
    public class ProcessRelation 
    { 
     [Key, Column("AreaId", Order = 0)] 
     public int AreaId { get; set; } 
     [Key, Column("ModId", Order = 1)] 
     public int ModId { get; set; } 
    [Key, Column("PriceSourceTypeId", Order = 2)] 
     public int PriceSourceTypeId { get; set; } 
     public int CrtUsrnm { get; set; } 
     public DateTime CrtTmstmp { get; set; } 
     public int LcUsrnm { get; set; } 
     public DateTime LcTmstmp { get; set; } 

     [ForeignKey("PriceSourceTypeId")] 
     public virtual BillingProjectCode PriceSourceTypeCode { get; set; } 
     [ForeignKey("AreaId")] 
     public virtual JobBusinessArea JobBusinessArea { get; set; } 

     //TODO: 
     //Depending on the PriceSourceTypeId i would like create reference to PriceListProcessMod Table 
    //or EstimateProcessMod Table 
     //[ForeignKey("ModId")] 
     //public virtual PriceListProcessMod PriceListProcessMod { get; set; } 
     // or 
     //public virtual EstimateProcessMod EstimateProcessMod { get; set; } 
    } 


[Table("PriceListProcessMod")] 
    public class PriceListProcessMod 
    { 
     [Key, Column("ModId", Order = 0)] 
     public int ModId { get; set; } 
     public int ProcessId { get; set; } 
     public int Quantity { get; set; } 
     public bool IsIncluded { get; set; } 
     public int CrtUsrnm { get; set; } 
     public DateTime CrtTmstmp { get; set; } 
     public int LcUsrnm { get; set; } 
     public DateTime LcTmstmp { get; set; } 
     public decimal? CommisionPercentage { get; set; } 
     public bool? IsProportionalyFixed { get; set; } 
     [ForeignKey("ProcessId")] 
     public virtual PriceListProcess PriceListProcess { get; set; } 
    } 

[Table("EstimateProcessMod")] 
    public class EstimateProcessMod 
    { 
     [Key, Column("ModId", Order = 0)] 
     public int ModId { get; set; } 
     public int? SplitId { get; set; } 
     public int ProcessId { get; set; } 
     public int Quantity { get; set; } 
     public bool IsIncluded { get; set; } 
     public int CrtUsrnm { get; set; } 
     public DateTime CrtTmstmp { get; set; } 
     public int LcUsrnm { get; set; } 
     public DateTime LcTmstmp { get; set; } 
     public decimal? CommisionPercentage { get; set; } 
     public bool? IsProportionalyFixed { get; set; } 
     [ForeignKey("ProcessId")] 
     public virtual EstimateProcess EstimateProcess{ get; set; } 
    } 

[Table("JobBusinessArea")] 
    public class JobBusinessArea 
    { 
     [Key, Column("JobBusinessAreaId", Order = 0)] 
     public int JobBusinessAreaId { get; set; } 

     public int CrtUsrnm { get; set; } 
     public DateTime CrtTmstmp { get; set; } 
     public int LcUsrnm { get; set; } 
     public DateTime LcTmstmp { get; set; } 

     public int ProjectId { get; set; } 
     public int AreaTypeId { get; set; } 
     public int SourceId { get; set; } 

    } 
+0

SQL 또는 EF에는 조건부 관계가 없습니다. FK 열은 정확히 하나의 대상 PK 열을 대상으로합니다. 귀하의 경우, null 가능 탐색 속성/FK를 사용해야하며 TPH 상속과 쌍을 이룰 수도 있습니다. – DevilSuichiro

답변

0

EstimateProcessMod 및 PriceListProcessMod는 공통점이 많습니다. 나는 그것을 활용하고 두 엔티티를 하나의 테이블 ProcessMod에 저장하도록 제안 할 것이다. ProcessMod 추상 클래스를 작성하고 Table attribute으로 주석을 달아서. 구체적인 클래스는 차이점 만 정의합니다.

[Table("ProcessMod")] 
public abstract class ProcessMod 
{ 
    [Key, Column("ModId", Order = 0)] 
    public int ModId { get; set; } 
    public int ProcessId { get; set; } 
    public int Quantity { get; set; } 
    public bool IsIncluded { get; set; } 
    public int CrtUsrnm { get; set; } 
    public DateTime CrtTmstmp { get; set; } 
    public int LcUsrnm { get; set; } 
    public DateTime LcTmstmp { get; set; } 
    public decimal? CommisionPercentage { get; set; } 
    public bool? IsProportionalyFixed { get; set; } 
} 

public class EstimateProcessMod : ProcessMod 
{ 
    public int? SplitId { get; set; } 
    [ForeignKey("ProcessId")] 
    public virtual EstimateProcess EstimateProcess{ get; set; } 
} 

public class PriceListProcessMod : ProcessMod 
{ 
    [ForeignKey("ProcessId")] 
    public virtual PriceListProcess PriceListProcess { get; set; } 
} 

[Table("ProcessRelation")] 
public class ProcessRelation 
{ 
    [Key, Column("AreaId", Order = 0)] 
    public int AreaId { get; set; } 
    [Key, Column("ModId", Order = 1)] 
    public int ModId { get; set; } 
    [Key, Column("PriceSourceTypeId", Order = 2)] 
    public int PriceSourceTypeId { get; set; } 
    public int CrtUsrnm { get; set; } 
    public DateTime CrtTmstmp { get; set; } 
    public int LcUsrnm { get; set; } 
    public DateTime LcTmstmp { get; set; } 

    [ForeignKey("PriceSourceTypeId")] 
    public virtual BillingProjectCode PriceSourceTypeCode { get; set; } 
    [ForeignKey("AreaId")] 
    public virtual JobBusinessArea JobBusinessArea { get; set; } 

    [ForeignKey("ModId")] 
    public virtual ProcessMod ProcessMod { get; set; } 
} 

당신은 아마 ProcessRelation 클래스에 PriceSourceTypeId 제거하고 ProcessMod 클래스로 이동하고 판별을 만들거나 어쩌면 당신은 전혀 사용할 필요가 없습니다 수 있습니다. 정말 나머지 응용 프로그램에 따라 다릅니다.

어떻게 작동하는지 이해하려면 Table per Hierarchy에 관심이 있습니다.