EF 6. Im trying to figure out the most fluid way to update an EF graph. So i have a ninja model with a list of NinjaEquipments. Lets say the ninja has three equipments like the following
Ninja (Id:99)
- EquipmentID 1, name: "Tool"
- EquipmentID 2, name: "Sword"
- EquipmentID 3, name: "Pokemon"
I send the model to a web application to be edited and the following is returned.
Ninja (Id:99)
- EquipmentID 1, name: "Tool"
- EquipmentID 3, name: "My Favourite Pokemon"
- EquipmentID 0 because its new, name: "Unicorn"
So what has happened is ninja has
- Lost item with id 2 "Sword"
- renamed item with id 3 to "My Favourite Pokemon"
- gained a new item with name "Unicorn"
How would i go about updating the model in the db context the easiest way. The only way i would be able to do it know is to look for new items, look for changed items and compare the updated model with the saved model to see wich items have been deleted. Is this the correct way or is there a way to give EF the new updated model and tell it that NinjaEquipments should reflect it?
Classes:
public class NinjaEquipment
{
public int Id { get; set; }
public string Name { get; set; }
public EquipmentType Type { get; set; }
[Required]
public Ninja Ninja { get; set; }
}
public class Ninja
{
public Ninja(Clan clan )
{
Clan = clan;
EquipmentOwned = new List<NinjaEquipment>();
}
public Ninja()
{
EquipmentOwned = new List<NinjaEquipment>();
}
public int Id { get; set; }
public Clan Clan { get; set; }
public int ClanId { get; set; }
public bool ServedInOnibawan { get; set; }
public string Name { get; set; }
public List<NinjaEquipment> EquipmentOwned { get; set; }
}