Reputation: 256
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?
Upvotes: 1
Views: 3694
Reputation: 10865
Firstly, I think your database relations would like this, am I correct?
Offers
has a simple primary key of Offer_ID
Heads
has a composite primary key of Head_ID
and Offer_ID
Heads
has a foreign key to Offers
Lines
has a composite primary key of Line_ID
, Head_ID
and Offer_ID
Lines
has a foreign key to Heads
Assuming I am correct in my reading of your question...
You are nearly perfect with your code. The only thing is the Line
class. Here you are specifying:
<ForeignKey("Head_ID")>
Public Property Head As Head
but instead you need:
<ForeignKey("Head_ID, Offer_ID")>
Public Property Head As Head
All down to a tricky comma-separated string!
The reason is that the primary key on Heads is a composite so you must specify all the columns to make the relationship correct.
Upvotes: 2
Reputation: 1105
Your Head class has a foreign key comprising of Head_Id and and Offer Id. Your Line class needs to reference the Head via a foreign key comprised of Head Id and Offer Id.
You can use data annotations and the Column Order syntax. You should be able to find the exact syntax on MSDN.
Upvotes: 1