When I insert a new object to the DB I tie it with a relationship to another, by doing another db lookup, I’d like to remove this extra step for efficiency, I.E How can I set a relationship to another using just the ID, rather than having to fetch the whole object from the DB? e.g.:
Car C = new Car();
C.Person = db.person.find(PersonID); //unnecessary lookup
//I'd like to do: C.setPersonID(personID);
db.cars.add(C);
db.saveChanges();
where model:
class Car
{
public virtual Person Person { get; set; }
}
Assuming that its a one-to-one relationship, you could do this:
Then to add a new car with a person using only the person’s Id and also assuming that the person with that Id is existing:
You may find this link useful.