开发者

How update full object with linq to entities?

I want update object with linq to entities, like this :

  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();
                }
}

But It doesn't...

If I does :

  public ActionResult Subm开发者_StackOverflow社区itPool(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();
                }
}

It does! But I want Update Full object. How do?


The line

IsPool = Pool;  

does nothing to the data within; it simply changes the reference.
After that line, both IsPool and Pool point to the same object in the heap; not useful in your case. You have to specifically assign each member from one class to the other.


use AutoMapper for map between your view models and linq entities

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜