我正在使用 Sql 2008 r2 和 Visual Studio 2010 和 EF 4.4。而且我在运行代码时遇到了这个错误。代码应该解释数据库关系。
在模型生成期间检测到一个或多个验证错误: \tSystem.Data.Entity.Edm.EdmAssociationConstraint: : 关系约束中 >Dependent 和 Principal Roles 中的属性数量必须相同。
我想使用数据注释解决这个问题。我在做什么?
'提供
Public Class Offer
<Key(), DatabaseGenerated(DatabaseGeneratedOption.None)>
Public Property Offer_ID As Integer
Public Property Name As String
End Class
'头
Public Class Head
<Key(), Column(Order:=0), DatabaseGenerated(DatabaseGeneratedOption.None)>
Public Property Head_ID As Integer
<ForeignKey("Offer_ID")>
Public Property Offer As Offer
<Key(), Column(Order:=1)>
Public Property Offer_ID As Integer
Public Property Name As String
End Class
'线
Public Class Line
<Key(), Column(Order:=0), DatabaseGenerated(DatabaseGeneratedOption.None)>
Public Property Line_ID As Integer
<ForeignKey("Head_ID")>
Public Property Head As Head
<Key(), Column(Order:=1)>
Public Property Head_ID As Integer
<ForeignKey("Offer_ID")>
Public Property Offer As Offer
<Key(), Column(Order:=2)>
Public Property Offer_ID As Integer
Public Property Name As String
End Class
'数据库上下文
Public Class DatabaseContext
Inherits DbContext
Public Sub New(p_ConnectionString As String)
MyBase.New(p_ConnectionString)
End Sub
Public Property Offers As DbSet(Of Offer)
Public Property Heads As DbSet(Of Head)
Public Property Lines As DbSet(Of Line)
End Class
'创建一个简单的例子
Private Shared Sub CreateME()
Dim offer As New Offer
offer.Name = "Offer1"
offer.Offer_ID = 1
Dim head As New Head
head.Head_ID = 1
head.Name = "head1"
head.Offer = offer
head.Offer_ID = offer.Offer_ID
Dim line As New Line
line.Head = head
line.Head_ID = head.Head_ID
line.Line_Id = 1
line.Name = "line1"
line.Offer = offer
line.Offer_ID = offer.Offer_ID
Using context = New DatabaseContext(GetConnectionString())
context.Offers.Add(offer)
context.Heads.Add(head)
context.Lines.Add(line)
context.SaveChanges()
End Using
End Sub
所以问题是我可以使用数据注释来解决这个问题吗?
我是否必须使用此处解释的模型构建器: 如何修复:关系约束中的从属角色和主要角色中的属性数量必须相同?