c# - How to add navigation property in the Entity Framework -


i trying add new repository class table called personfieldgroupdomain how looks like:

enter image description here

so have 2 fields table. went entity framework model designer , couldn't add table designer using update model database, not visible. when see other tables have dependency on it, can see navigational property.

so, need add new record since cannot reach context directly others, tried adding using "attach" method this:

var fieldgroupset = fieldgroupsets.single(t=>t.fieldgroupsetid == 1); fieldgroupset.personfieldgroupdomainreference.attach(domains.single(t=>t.domainid == 2)); 

and code didn't work. @ least tested on linqpad using same context assembly using in actual project.

this article seems talking of ways can use http://msdn.microsoft.com/en-us/data/jj592676.aspx none of them seemed working me.

any working idea appreciated.

it looks table join table. ef hide these in designer view. notice if add surrogate key or table, you'll start seeing in designer entity.

to use it, should simple as

var fieldgroupset = fieldgroupsets.single(t => t.fieldgroupsetid == 1); var domain = domains.single(t => t.domainid == 2); fieldgroupset.domain = domain; context.savechanges(); 

edited op:

my note: sorry editing answer, in order mark answer, need put actual code used worked follows logic:

var fieldgroupset = fieldgroupsets.single(t=>t.fieldgroupsetid == 1); var domain = domains.single(t => t.domainid == 3); fieldgroupset.personfieldgroupdomain = domain; savechanges(); 

in code above, since personfieldgroupdomain navigation property, directly goes domain type of domain when reach fieldgroupset.personfieldgroupdomain property. seen in code above, directly setting existing domain object , after saving changes, changes become persistent in database , worked!


Comments

Popular posts from this blog

plot - Remove Objects from Legend When You Have Also Used Fit, Matlab -

java - Why does my date parsing return a weird date? -

Need help in packaging app using TideSDK on Windows -