Пример #1
0
        //Insert Province
        public static void InsertProvince()
        {
            var province = new Province {
                ProvinceName = "浙江省"
            };

            province.Donators.Add(new Donator
            {
                Name       = "星空夜焰",
                Amount     = 50m,
                DonateDate = DateTime.Parse("2016-7-12")
            });
            province.Donators.Add(new Donator
            {
                Name       = "伟涛",
                Amount     = 30m,
                DonateDate = DateTime.Parse("2016-7-12")
            });

            using (var db = new DonatorsContext())
            {
                db.Provinces.Add(province);
                db.SaveChanges();
            }

            Console.WriteLine("新建对象,然后添加到数据库上下文,这个表明了EF会追踪当时上下文中为attached或者added状态的实体");
        }
Пример #2
0
 //桌面应用,更新Dontor
 public static void 桌面应用UpdateDonator()
 {
     using (var db = new DonatorsContext())
     {
         var donator = db.Donators.Find(4);
         donator.Name = "醉千秋"; //我想把“醉、千秋”中的顿号去掉
         db.SaveChanges();
     }
     Console.WriteLine("桌面应用更新Donators, ");
 }
Пример #3
0
        //常规删除数据
        public static void 常规删除数据()
        {
            using (var db = new DonatorsContext())
            {
                PrintAllDonators(db);
                FluentConsole.Green.Line("删除后的数据如下:");
                var toDelete = db.Provinces.Find(2);
                toDelete.Donators.ToList().ForEach(d => db.Donators.Remove(d));
                db.Provinces.Remove(toDelete);
                db.SaveChanges();

                PrintAllDonators(db);
            }
        }
Пример #4
0
 //添加捐赠者
 public static void AddDonator()
 {
     using (var context = new DonatorsContext())
     {
         var donator = new Donator
         {
             Name       = "小黄",
             Amount     = 12m,
             DonateDate = DateTime.MaxValue,
             Province   = new Province
             {
                 ProvinceName = "新疆"
             }
         };
         context.Donators.Add(donator);
         context.SaveChanges();
     }
 }
Пример #5
0
        public static void Web应用另一种更新操作()
        {
            var donator = new Donator
            {
                Id         = 5,
                Name       = "雪茄",
                Amount     = 12m,
                DonateDate = DateTime.Parse("2016-7-13")
            };

            using (var db = new DonatorsContext())
            {
                db.Donators.Attach(donator);
                //db.Entry(donator).State=EntityState.Modified;//这句可以作为第二种方法替换上面一句代码
                donator.Name = "秦皇岛-雪茄";
                db.SaveChanges();
            }

            Console.WriteLine("Web应用另一种更新操作完成!");
        }
Пример #6
0
        //Insert Province ,修改实体的状态为added
        public static void InsertProvince2()
        {
            var province = new Province {
                ProvinceName = "广东省"
            };

            province.Donators.Add(new Donator
            {
                Name       = "求余",
                Amount     = 30,
                DonateDate = DateTime.Parse("2016-7-12")
            });

            using (var db = new DonatorsContext())
            {
                db.Entry(province).State = EntityState.Added;
                db.SaveChanges();
            }
            Console.WriteLine("InsertProvince,修改实体的状态为added");
        }
Пример #7
0
        //设置实体状态删除
        public static void 设置实体状态删除()
        {
            //通过设置实体状态删除
            var toDeleteProvince = new Province {
                Id = 1
            };

            toDeleteProvince.Donators.Add(new Donator
            {
                Id = 2
            });
            toDeleteProvince.Donators.Add(new Donator
            {
                Id = 3
            });
            toDeleteProvince.Donators.Add(new Donator
            {
                Id = 4
            });

            using (var db = new DonatorsContext())
            {
                PrintAllDonators(db); //删除前先输出现有的数据,不能写在下面的using语句中,否则Attach方法会报错,原因我相信你已经可以思考出来了
            }
            using (var db = new DonatorsContext())
            {
                db.Provinces.Attach(toDeleteProvince);
                foreach (var donator in toDeleteProvince.Donators.ToList())
                {
                    db.Entry(donator).State = EntityState.Deleted;
                }
                db.Entry(toDeleteProvince).State = EntityState.Deleted; //删除子实体再删除父实体。
                db.SaveChanges();
                Console.WriteLine("删除之后的数据如下:\r\n");
                PrintAllDonators(db);
            }
        }
Пример #8
0
        //Web应用,更新Province
        public static void Web应用UpdateProvince()
        {
            var province = new Province {
                Id = 2, ProvinceName = "山东省更新"
            };

            province.Donators.Add(new Donator
            {
                Name       = "醉、千秋", //再改回来
                Id         = 4,
                Amount     = 12m,
                DonateDate = DateTime.Parse("2015-7-12")
            });
            using (var db = new DonatorsContext())
            {
                db.Entry(province).State = EntityState.Modified;
                foreach (var donator in province.Donators)
                {
                    db.Entry(donator).State = EntityState.Modified;
                }
                db.SaveChanges();
            }
            Console.WriteLine("Web应用更新Province");
        }