c# - Adding objects via DbContext duplicates related objects -


i have object, call thing, has many-to-one relationship object, call person. how can add thing using dbcontext without duplicating associated person. thing has foreign key person called personid.

public class thing {   public long id { get; set; }   public long personid { get; set; } }  public class person {   public long id { get; set; } } 

i tried this:

context.things.add(newthing); context.savechanges(); 

i tried this:

person person = new person() { id = newthing.personid }; context.persons.attach(person); context.things.add(newthing); context.savechanges(); 

not sure if understand question correctly try:

context.entry(person).state = system.data.entitystate.unchanged; 

this tells ef entity unchanged , hence not attempt save it.


Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -