我希望使用linq更新对象到实体,如下所示:
public ActionResult SubmitPool(SwimmingPool Pool)
{
SwimmingPool IsPool = (from sp in db.SwimmingPool
where sp.Id == Pool.Id
select sp).First();
if (IsPool != null) {
IsPool = Pool;
db.SaveChanges();
}
}
但它没有......
如果我这样做:
public ActionResult SubmitPool(SwimmingPool Pool)
{
SwimmingPool IsPool = (from sp in db.SwimmingPool
where sp.Id == Pool.Id
select sp).First();
if (IsPool != null) {
----> IsPool.Name = Pool.Name;
db.SaveChanges();
}
}
确实如此!但我想要Update Full对象。怎么办?
答案 0 :(得分:2)
该行
IsPool = Pool;
对其中的数据没有任何作用;它只是改变了参考
在该行之后,IsPool
和Pool
都指向堆中的同一个对象;在你的情况下没用。
您必须专门将每个成员从一个类分配给另一个类。
答案 1 :(得分:1)
使用AutoMapper查看视图模型和linq实体之间的地图