【发布时间】:2017-12-02 02:57:04
【问题描述】:
我检查了this one 和其他相关问题,但找不到适合我的案例的有效解决方案。
所以,我有 3 个实体,PrintingMethod、PrintingSubMethod 和 PrintPricing。规则是:
-
PrintingMethod有一个PrintingSubMethod的集合。 >> 没问题。 -
PrintingMethod可以有零个或一个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 或 Fluent API 来实现?如果不是,请随时提出替代方案。
注意事项:
- 为简洁起见,删除了不相关的字段和 DataAnnotations。
- 我使用的是 Entity Framework 6.0。
- 虽然我的代码是用 VB 编写的,但也欢迎任何 C# 答案。
【问题讨论】:
标签: c# .net vb.net entity-framework one-to-one