简体   繁体   中英

Set Foreign key value in EF1 (asp.net 3.5)

I have to down grade my app from .net 4 to 3.5 to get it to run on our server. :(

Anyway EF "Independent Association" - http://blogs.msdn.com/b/efdesign/archive/2009/03/16/foreign-keys-in-the-entity-framework.aspx

...but now I can't figure out how to set the value of a foreign key because it no longer appears in my Entity.

Can anyone advise how I can do this please?

AFAIK, you cannot set the value directly, but must use the navigation property instead. So if you have a Parent row you would like to set the foreign key of to 15, it would look like this:

var Child = (from c in context.Children
             where id = 15).Single();
Parent.Child = Child;
context.SaveChanges();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM