Wednesday, 15 August 2012

.net - Fix One or more validation errors were detected during model generation using dataannotation -



.net - Fix One or more validation errors were detected during model generation using dataannotation -

iam using sql 2008 r2 , visual studio 2010 , ef 4.4. , iam getting error runnning code longer down. code should explain database relations.

one or more validation errors detected during model generation: \tsystem.data.entity.edm.edmassociationconstraint: : number of properties in >dependent , principal roles in relationship constraint must identical.

i want solve using dataannotation. doing wroing?

'offer

public class offer <key(), databasegenerated(databasegeneratedoption.none)> public property offer_id integer public property name string end class

'head

public class head <key(), column(order:=0), databasegenerated(databasegeneratedoption.none)> public property head_id integer <foreignkey("offer_id")> public property offer offer <key(), column(order:=1)> public property offer_id integer public property name string end class

'line

public class line <key(), column(order:=0), databasegenerated(databasegeneratedoption.none)> public property line_id integer <foreignkey("head_id")> public property head head <key(), column(order:=1)> public property head_id integer <foreignkey("offer_id")> public property offer offer <key(), column(order:=2)> public property offer_id integer public property name string end class

'dbcontext

public class databasecontext inherits dbcontext public sub new(p_connectionstring string) mybase.new(p_connectionstring) end sub public property offers dbset(of offer) public property heads dbset(of head) public property lines dbset(of line) end class

'create simple example

private shared sub createme() dim offer new offer offer.name = "offer1" offer.offer_id = 1 dim head new head head.head_id = 1 head.name = "head1" head.offer = offer head.offer_id = offer.offer_id dim line 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 question can solve using info annotation?

do have utilize modelbuilder explained in here: how fix: number of properties in dependent , principal roles in relationship constraint must identical?

firstly, think database relations this, correct?

offers has simple primary key of offer_id heads has composite primary key of head_id , offer_id heads has foreign key offers lines has composite primary key of line_id, head_id , offer_id lines has foreign key heads

assuming right in reading of question...

you perfect code. thing line class. here specifying:

<foreignkey("head_id")> public property head head

but instead need:

<foreignkey("head_id, offer_id")> public property head head

all downwards tricky comma-separated string!

the reason primary key on heads composite must specify columns create relationship correct.

.net sql ef-code-first

No comments:

Post a Comment