Сценарий:организация обновления и связанные с ними объекты
class Foo {
[Key]
public int Id { get; set; }
public List<Bar> Bars { get; set; }
}
class Bar {
[Key]
public int Id { get; set; }
public string Name { get; set; }
}
я должен реализовать простой CRUD-опа, как это:
public void InsertOrUpdateFoo(Foo foo) {
var db = new MyContext();
//here some pseudocode
if (foo exists) {
d.Foos.Add(foo);
} else {
//here which is the best solution?
//a good tradeoff between performance and code semplicity
//case 1: delete first and add
db.Foos.Remove(oldFoo);
db.Add(foo);
db.SaveChanges();
//case 2: there is some functionality that allows you to update the entity like:
db.Modify(oldEntity, newEntity);
}
db.Dispose();
}
В сценарии обновления, который, кажется, чтобы быть лучшим выбором?
- Удаление и добавление
- Управление вручную обновление (Foreach subentities)
- Некоторые другие методы ??