Example #1
0
        private static void Method09()
        {
            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);

                db.Update<Member>(m => new { m.Password }, member);
                int count = db.SaveChanges(false);
                Console.WriteLine("操作结果:{0}", count > 0 ? "更新成功。" : "未更新。");

                Member newMember = db.Members.Single(m => m.Id == 1);
                Console.WriteLine("更新后:{0}。", newMember.Password);
            }
        }
Example #2
0
        //情景一:取出数据更新后直接保存
        private static void Method01()
        {
            using (var db = new DataContext())
            {
                const string userName = "******";
                Member oldMember = db.Members.Single(m => m.UserName == userName);
                Console.WriteLine("更新前:{0}。", oldMember.AddDate);

                oldMember.AddDate = oldMember.AddDate.AddMinutes(10);
                db.Update(oldMember);
                int count = db.SaveChanges();
                Console.WriteLine("操作结果:{0}", count > 0 ? "更新成功。" : "未更新。");

                Member newMember = db.Members.Single(m => m.UserName == userName);
                Console.WriteLine("更新后:{0}。", newMember.AddDate);
            }
        }
Example #3
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);
            }
        }
Example #4
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);
            }
        }
Example #5
0
        private static void Method05()
        {
            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);

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

                Member newMember = db2.Members.Single(m => m.UserName == userName);
                Console.WriteLine("更新后:{0}。", newMember.AddDate);
            }
        }
Example #6
0
        static void Main(string[] args)
        {
            try
            {
                var migrate = new MigrateDatabaseToLatestVersion<DataContext, Configuration>();
                Database.SetInitializer(migrate);
                using (var db = new DataContext())
                {
                    Console.WriteLine("数据初始化完成:部门信息{0}条,角色信息{1}条,人员信息{2}条", db.Departments.Count(), db.Roles.Count(), db.Members.Count());
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(new ExceptionMessage(e));
            }
        Reinput:
            try
            {
                Console.WriteLine("请输入:功能1-n,退出0");
                int input;
                if (!int.TryParse(Console.ReadLine(), out input))
                {
                    goto Reinput;
                }

                switch (input)
                {
                    case 0:
                        break;
                    case 1:
                        Method01();
                        goto Reinput;
                    case 2:
                        Method02();
                        goto Reinput;
                    case 3:
                        Method03();
                        goto Reinput;
                    case 4:
                        Method04();
                        goto Reinput;
                    case 5:
                        Method05();
                        goto Reinput;
                    case 6:
                        Method06();
                        goto Reinput;
                    case 7:
                        Method07();
                        goto Reinput;
                    case 8:
                        Method08();
                        goto Reinput;
                    case 9:
                        Method09();
                        goto Reinput;
                    default:
                        Console.WriteLine("编号{0}的功能不存在。\r\n", input);
                        goto Reinput;
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(new ExceptionMessage(e));
                goto Reinput;
            }
        }
Example #7
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);
            }
        }