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
Post a Comment