Beispiel #1
0
        private static void Method08()
        {
            Member member = new Member { Id = 1, Password = "******" + DateTime.Now.Second };
            using (var db = new DataContext())
            {
                //先查询一次,让上下文中存在相同主键的对象
                Member oldMember = db.Members.Single(m => m.Id == 1);
                Console.WriteLine("更新前:{0}。", oldMember.AddDate);

                try
                {
                    DbEntityEntry<Member> entry = db.Entry(member);
                    entry.State = EntityState.Unchanged;
                    entry.Property("Password").IsModified = true;
                }
                catch (InvalidOperationException)
                {
                    ObjectContext objectContext = ((IObjectContextAdapter)db).ObjectContext;
                    ObjectStateEntry objectEntry = objectContext.ObjectStateManager.GetObjectStateEntry(oldMember);
                    objectEntry.ApplyCurrentValues(member);
                    objectEntry.ChangeState(EntityState.Unchanged);
                    objectEntry.SetModifiedProperty("Password");
                }
                db.Configuration.ValidateOnSaveEnabled = false;
                int count = db.SaveChanges();
                db.Configuration.ValidateOnSaveEnabled = true;
                Console.WriteLine("操作结果:{0}", count > 0 ? "更新成功。" : "未更新。");

                Member newMember = db.Members.Single(m => m.Id == 1);
                Console.WriteLine("更新后:{0}。", newMember.Password);
            }
        }
Beispiel #2
0
        //针对Method03的InvalidOperationException异常的处理方案
        private static void Method04()
        {
            const string userName = "******";

            Member updateMember;
            using (var db1 = new DataContext())
            {
                updateMember = db1.Members.Single(m => m.UserName == userName);
            }
            updateMember.AddDate = DateTime.Now;

            using (var db2 = new DataContext())
            {
                //先查询一次,让上下文中存在相同主键的对象
                Member oldMember = db2.Members.Single(m => m.UserName == userName);
                Console.WriteLine("更新前:{0}。", oldMember.AddDate);

                DbEntityEntry<Member> entry = db2.Entry(oldMember);
                entry.CurrentValues.SetValues(updateMember);
                int count = db2.SaveChanges();
                Console.WriteLine("操作结果:{0}", count > 0 ? "更新成功。" : "未更新。");

                Member newMember = db2.Members.Single(m => m.UserName == userName);
                Console.WriteLine("更新后:{0}。", newMember.AddDate);
            }
        }
Beispiel #3
0
        private static void Method06()
        {
            Member member = new Member { Id = 1, Password = "******" + DateTime.Now.Second };
            using (var db = new DataContext())
            {
                DbEntityEntry<Member> entry = db.Entry(member);
                entry.State = EntityState.Unchanged;
                entry.Property("Password").IsModified = true;
                db.Configuration.ValidateOnSaveEnabled = false;
                int count = db.SaveChanges();
                db.Configuration.ValidateOnSaveEnabled = true;
                Console.WriteLine("操作结果:{0}", count > 0 ? "更新成功。" : "未更新。");

                Member newMember = db.Members.Single(m => m.Id == 1);
                Console.WriteLine("更新后:{0}。", newMember.Password);
            }
        }
Beispiel #4
0
        //情景三:在情景二的基础上,上下文2中已存在与外来实体主键相同的数据了,引发InvalidOperationException异常
        private static void Method03()
        {
            const string userName = "******";

            Member updateMember;
            using (var db1 = new DataContext())
            {
                updateMember = db1.Members.Single(m => m.UserName == userName);
            }
            updateMember.AddDate = DateTime.Now;

            using (var db2 = new DataContext())
            {
                //先查询一次,让上下文中存在相同主键的对象
                Member oldMember = db2.Members.Single(m => m.UserName == userName);
                Console.WriteLine("更新前:{0}。", oldMember.AddDate);
                var dbset = db2.Members.Local;

                db2.Members.Attach(updateMember);
                DbEntityEntry<Member> entry = db2.Entry(updateMember);
                Console.WriteLine("Attach成功后的状态:{0}", entry.State); //附加成功之后,状态为EntityState.Unchanged
                entry.State = EntityState.Modified;
                int count = db2.SaveChanges();
                Console.WriteLine("操作结果:{0}", count > 0 ? "更新成功。" : "未更新。");

                Member newMember = db2.Members.Single(m => m.UserName == userName);
                Console.WriteLine("更新后:{0}。", newMember.AddDate);
            }
        }