2017-12-02 2 views
0

this one 및 기타 관련 질문을 확인했지만 제 사례의 해결 방법을 찾을 수 없습니다.복수 선택형 일대일 릴레이션 중 어느 하나가 필요합니다.

그래서 세 개의 엔티티, PrintingMethod, PrintingSubMethodPrintPricing이 있습니다. 규칙은 다음과 같습니다.

  • PrintingMethodPrintingSubMethod입니다. >> 아무 문제 없어.
  • PrintingMethod이고 0 또는 하나 PrintPricing 일 수 있습니다.
  • PrintingSubMethod 하나는 PrintPricing이어야합니다.
  • PrintPricing중 하나PrintingMethod또는PrintingSubMethod와 연결해야합니다.

여기서 처음 두 기관의 :

Public Class PrintingMethod 
    Public Property Id As Integer 
    Public Property Name As String 
    Public Property SupportsAdditionalColors As Boolean 

    Public Overridable Property SubMethods As ICollection(Of PrintingSubMethod) 
    Public Overridable Property AdditionalColorPricing As PrintPricing 
End Class 


Public Class PrintingSubMethod 
    Public Property Id As Integer 
    Public Property Name As String 

    Public Property PrintingMethodId As Integer 

    <ForeignKey("PrintingMethodId")> 
    Public Overridable Property ParentMethod As PrintingMethod 

    Public Overridable Property Pricing As PrintPricing 
End Class 

그리고 여기에 세 번째의 그림입니다 :

Public Class PrintPricing 
    Public Property Id As Integer 

    <ForeignKey("LinkedSubMethod")> 
    Public Property SubMethodId As Integer 

    <ForeignKey("LinkedMethod")> 
    Public Property MethodId As Integer 

    Public Overridable Property LinkedSubMethod As PrintingMethodCode 
    Public Overridable Property LinkedMethod As PrintingMethod 
End Class 

그래서,이 DataAnnotations이나 유창함 API를 사용하여 달성 할 수있다? 그렇지 않다면 대안을 제안하십시오.

주 :

  • 무관 필드와 DataAnnotations은 간결함을 제거했다.
  • 저는 Entity Framework 6.0을 사용하고 있습니다.
  • 내 코드는 VB이지만 모든 C# 응답도 환영합니다.

답변

1

내가 바로 질문을 읽을 경우, 다음과 같이 보일 것입니다 귀하의 관계를 그래프 :

PrintingMethod  [1:n] PrintingSubMethod 
PrintingMethod [0..1:1] PrintPricing 
PrintingSubMethod [0..1:1] PrintPricing 

그것은 주요 기업이 항상 먼저 표시해야하기 때문에 하나 개의 관계 순수한 사람이 불가능하다고 지적 가치 (종속 엔터티가 존재하지 않아서 우리는 이후의 제약 조건 인 1:1을 무효화합니다). 따라서 실제적으로 1:1은 1 대 0 또는 1 대 관계가되는 경향이 있습니다.

PrintingSubMethod

PrintPricing

그래서이 규칙을 적용하는 것은 다른 계층에서 그것을 처리 할 필요가 있어야합니다.


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

    // You may want to uncomment the "navigation" properties, 
    // but I'm going to leave it commented so you know this is 
    // not required for it to establish the relationship correctly, 
    // because this class represents the principal end of the relationship 
    // public virtual PrintingMethod Method { get; set; } 
    // public virtual PrintingSubMethod SubMethod { get; set; } 
} 

public class PrintingMethod 
{ 
    [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)] 
    [ForeignKey("Pricing")] // foreign keys go on the dependent ends for 0:1 relationships 
    public int Id { get; set; } 
    public virtual List<PrintingSubMethod> SubMethods { get; set; } 

    public virtual PrintPricing Pricing { get; set; } 
} 

public class PrintingSubMethod 
{ 
    [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)] 
    [ForeignKey("Pricing")] 
    public int Id { get; set; } 
    public virtual PrintingMethod Method { get; set; } 

    public virtual PrintPricing Pricing { get; set; } 
} 

당신이 원하신다면 당신은 수동으로 PrintingMethod [1:n] PrintingSubMethod 관계에 대한 외부 키를 정의 할 수 있습니다

하지만이 옵션 그래서 EF는 그 자체로 그것을 알아낼 수있을만큼 똑똑하다. 나는 개인적으로 외래 키를 수동으로 정의하는 것을 선호한다.

+0

그래프에 대해 정확하며 다른 레이어에서 해당 규칙을 적용해도 아무런 문제가 없습니다. 그래도 제안 된 솔루션을 구현하는 방법을 아직 모르겠습니다. EF에 실제로 익숙하지 않기 때문에 가능하면 DataAnnotations를 사용하여 * 이것을 구현하는 방법을 자세히 설명하면 좋을 것입니다. 고맙습니다. –

+0

내가 신경 쓰는 부분은 동일한 테이블에 두 개의 0-1 관계를 만드는 방법입니다. DataAnnotations *를 사용하는 방법은 대상 테이블의'FK'를'PK'로 만드는 것입니다. –

+0

@AhmedAbdelhameed 몇 가지 코드를 추가했습니다. 핵심은 외래 키를 주 엔드가 아닌 종속 엔드에 추가하는 것입니다. 나는 그것을 실행하려고하지 않았기 때문에 자유롭게 편집 해보세요 :) – gldraphael

관련 문제