Iam using Sql 2008 r2 and visual studio 2010 and EF 4.4. And iam getting this error runnning the code longer down. The Code should explain the database relations.
One or more validation errors were detected during model generation: \tSystem.Data.Entity.Edm.EdmAssociationConstraint: : The number of properties in the >Dependent and Principal Roles in a relationship constraint must be identical.
I want to solve this using dataannotation. What am i doing wroing?
'Offer
Public Class Offer
<Key(), DatabaseGenerated(DatabaseGeneratedOption.None)>
Public Property Offer_ID As Integer
Public Property Name As String
End Class
'Head
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
'Line
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
'DbContext
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
'Create a simple example
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
So the question is can i solve this using data annotation?
Do i have to use the modelbuilder explained in here: How to fix: The number of properties in the Dependent and Principal Roles in a relationship constraint must be identical?