文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>Attaching and Detaching Entities

Attaching and Detaching Entities

时间:2011-06-16  来源:FAST_Michael

If the entity object is already inside the object context, the existing one is used; otherwise it is fetched newly from the database.Invoking the method ApplyCurrentValues() passes the modified entity

object to the object context,and if there are changes, then the changes are done within the existing entity with the same key inside the object context, and the EntityState is set to EntityState.Modified. Remember that the method ApplyCurrentValues() requires the object to exist within the object context; otherwise the new entity object is added with EntityState.

private static void DetachDemo()
        {
            using (var data = new Formula1Entities())
            {
                data.ObjectStateManager.ObjectStateManagerChanged +=
                      ObjectStateManager_ObjectStateManagerChanged;
                ObjectQuery<Racer> racers = data.Racers.Where("it.Lastname='Alonso'");
                Racer fernando = racers.First();
                EntityKey key = fernando.EntityKey;
                data.Racers.Detach(fernando);
                // Racer is now detached and can be changed independent of the object context
                fernando.Starts++;
                Racer originalObject = data.GetObjectByKey(key) as Racer;
                data.Racers.ApplyCurrentValues(fernando);

                
            }

        }
static void ObjectStateManager_ObjectStateManagerChanged(object sender,
             CollectionChangeEventArgs e)
       {
           Console.WriteLine("Object State change—action: {0}", e.Action);
           Racer r = e.Element as Racer;
           if (r != null)
               Console.WriteLine("Racer {0}", r.Lastname);
       }
相关阅读 更多 +
排行榜 更多 +
《天国:拯救2》汉斯坐在你的肩膀上MOD

《天国:拯救2》汉斯坐在你的肩膀上MOD

行业软件 下载
《小朋友齐打交2复刻版》 v2.14d升级档+未加密补丁[TENOKE]

《小朋友齐打交2复刻版》 v2.14d升级档+未加密补丁[TENOKE]

行业软件 下载
《无主之地4》更多坏蛋MOD

《无主之地4》更多坏蛋MOD

行业软件 下载