I have an object, I will call it Thing, that has a many-to-one relationship with another object, I will call it Person. How can I add Thing using DbContext without duplicating the associated Person. Thing has a foreign key to 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 also tried to do this:
Person person = new Person() { ID = newThing.PersonID };
context.Persons.Attach(person);
context.Things.Add(newThing);
context.SaveChanges();
newThingand populate its properties would likely make it much easier to answer the question.null(you have a navigation property, right?) you don't get duplicated records. You have a special situation which causes this behaviour and I'm just asking for more context to get any idea what could be the reason.