0
的情况是:更新实体和相关实体
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 OPS是这样的:
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子实体)
- 一些其他技术?