C# 重复记录实体框架6(通用更新)
我举了一个类似的例子 但在我的例子中,集合属性的值是在数据库中重新创建的C# 重复记录实体框架6(通用更新),c#,entity-framework,repository,C#,Entity Framework,Repository,我举了一个类似的例子 但在我的例子中,集合属性的值是在数据库中重新创建的 public void Update(T updateItem, string property) { using (var databaseContext = new TelesystemContext()) { T foundDal = databaseContext.Set<T>().Find(updateItem.Id);
public void Update(T updateItem, string property)
{
using (var databaseContext = new TelesystemContext())
{
T foundDal = databaseContext.Set<T>().Find(updateItem.Id);
var entry = databaseContext.Entry(foundDal);
entry.CurrentValues.SetValues(updateItem);
var collection = entry.Collection(property);
collection.Load();
collection.CurrentValue = typeof(T).GetProperty(property).GetValue(updateItem);
databaseContext.SaveChanges();
}
}
public void更新(T updateItem,string属性)
{
使用(var databaseContext=new TelesystemContext())
{
T foundDal=databaseContext.Set().Find(updateItem.Id);
var entry=databaseContext.entry(foundDal);
entry.CurrentValues.SetValues(updateItem);
var collection=entry.collection(属性);
collection.Load();
collection.CurrentValue=typeof(T).GetProperty(property).GetValue(updateItem);
databaseContext.SaveChanges();
}
}
属性在保存时重新创建updateItem,而不是向表中添加一堆“多对多”