c# - relationship one to one EntityFramework error -
i create 2 table in sql server 2008
table 1 : user( userid,name, firstname,login, password...) , pk : userid
table 2 : sessionuser(userid, date, adress) , pk : userid
the relationship between 2 tables set in sql server 2008, 1 1 relationship , foreign key in table sessionuser (fk: userid)
there users in user table (full lines)
when tried add session in session table shows me error:
entities in 'distributionssentities.sessionuser
' participate in 'fk_sessionuser_user
' relationship. 0 related 'user ' found. 1 'user ' expected.
code:
distributionssentities db = new distributionssentities(); sessionuser sessionuser = new sessionuser(); sessionuser.userid = 12; // id existe in user table sessionuser.date = "12-12-2012"; sessionuser.adressip = "192.168.1.1"; db.addtosessionuser(sessionuser); db.savechanges();
how resolve problem thanx.
just setting userid
won't trick.
you can either:
- select user database , set user
property
- create new user
desired id , attach context
- or, set foreign key reference directly:
sessionuser.userreference.entitykey = new system.data.entitykey(sessionuser.userreference.relationshipset.entitycontainer.name + "." + sessionuser.userreference.targetrolename, "id", id);
it's bit long, can make extension method comes in handy.
Comments
Post a Comment