I'm consuming a webservice which deserialises xml into model instances which are used to build the actual EF data models.
If I have this example class modelling a Property
with a Branch
:
public class Property
{
public int Id {get; set;}
public int Name {get; set;}
public string BranchId {get; set;}
[ForeignKey("BranchId")]
public string Branch {get; set;}
}
If the Branch
doesn't exist in the database, fine, EF inserts it. However, if it does, how can I instruct EF to update it instead?
I get from the examples, you Attach()
an entity to a DbSet
so that the framework knows not to insert it, but is there a way to do this auto-magically? e.g not have to write the bolierplate code which checks for the Branch
everytime I insert a Property
to know whether I need to Attach()
it?