Example #1
0
 public static void Test()
 {
     using (var scope = new TransactionScope())
     {
         try
         {
             Database.SetInitializer <BFDbContext>(null);
             using (var context = new BFDbContext())
             {
                 context.Database.Log = Console.WriteLine;
                 var face = context.Face.FirstOrDefault();
                 context.Entry(face.Photo).State    = EntityState.Deleted;
                 context.Entry(face.FaceType).State = EntityState.Deleted;
                 context.Entry(face).State          = EntityState.Deleted;
                 int c = context.SaveChanges();
                 Console.WriteLine(string.Format("删除的影响行数:{0}", c));
             }
             scope.Complete();
         }
         catch (Exception e)
         {
             Console.WriteLine(e.Message);
         }
     }
 }
Example #2
0
        public void TestFaceSelect()
        {
            using (var scope = new TransactionScope())
            {
                using (var context = new BFDbContext())
                {
                    try
                    {
                        //var sw = new StreamWriter(@"d:\Data.log") { AutoFlush=true };
                        //context.Database.Log = sw.Write;

                        var face = context.Face.FirstOrDefault();
                        // var face = context.Face.Find(5);

                        Assert.AreNotEqual(null, face);
                        Console.WriteLine(string.Format("FaceId:{0}", face.Id));
                        Console.WriteLine(string.Format("FaceName:{0}", face.Name));
                        Console.WriteLine(string.Format("TypeId:{0}", face.TypeId));
                        Console.WriteLine(string.Format("PhotoPath:{0}", face.Photo.Path));
                        Console.WriteLine(string.Format("TypeName:{0}", face.FaceType.Name));
                        scope.Complete();
                    }
                    catch (Exception e)
                    {
                        throw e;
                    }
                }
            }
        }
Example #3
0
        public static void Test()
        {
            using (var scope = new TransactionScope())
            {
                Database.SetInitializer <BFDbContext>(null);
                using (var context = new BFDbContext())
                {
                    context.Database.Log = Console.WriteLine;
                    try
                    {
                        //var p = context.Person.AsNoTracking().FirstOrDefault(d=>d.Id==1);
                        var p = context.Person.Find(1);
                        Console.WriteLine(string.Format("{0}---{1}", p.Nationality.Id, p.Nationality.Name));

                        //context.Person.Attach(p);
                        context.Entry(p.Nationality).State = EntityState.Deleted;

                        context.Entry(p).State = EntityState.Deleted;
                        //context.Entry(p).CurrentValues.SetValues = p;
                        int c = context.SaveChanges();

                        Console.WriteLine(string.Format("删除人员信息的返回值:{0}", c));
                        foreach (var n in context.Nationality)
                        {
                            Console.WriteLine(string.Format("国家信息--ID:{0},Name:{1}", n.Id, n.Name));
                        }
                        scope.Complete();
                    }
                    catch (Exception e) {
                        Console.WriteLine(e.Message);
                    }
                }
            }
        }
Example #4
0
        public void TestFaceDelete()
        {
            using (var scope = new TransactionScope())
            {
                using (var context = new BFDbContext())
                {
                    context.Database.Log = Console.WriteLine;
                    var face = context.Face.FirstOrDefault();

                    context.Entry(face.Photo).State = EntityState.Deleted;
                    context.Entry(face).State       = EntityState.Deleted;

                    int c = context.SaveChanges();
                    Assert.Greater(c, 0);
                    Console.WriteLine("删除数据的影响行数:{0}", c);
                    scope.Complete();
                }
            }
        }
Example #5
0
 public void TestFaceInsert()
 {
     using (var scope = new TransactionScope())
     {
         using (var context = new BFDbContext())
         {
             var detched = context.Configuration.AutoDetectChangesEnabled;
             context.Database.Log = Console.WriteLine;
             try
             {
                 //context.Configuration.UseDatabaseNullSemantics = false;
                 context.Configuration.AutoDetectChangesEnabled = false;
                 FaceType faceType = new FaceType()
                 {
                     Id = 1, Name = "自定义类型1"
                 };
                 Photo photo = new Photo()
                 {
                     FaceId = 1, Path = "http://localhost:8088/testPDF/20190410170225.pdf"
                 };
                 Face face = new Face()
                 {
                     Id       = 1,
                     Name     = "Face1",
                     TypeId   = 1,
                     FaceType = faceType,
                     Photo    = photo
                 };
                 context.Entry(face).State = EntityState.Added;
                 int c = context.SaveChanges();
                 Assert.Greater(c, 0);
                 Console.WriteLine("保存数据的影响行数:{0}", c);
                 scope.Complete();
             }
             catch (Exception e)
             {
                 Console.WriteLine(e.Message);
             }
         }
     }
 }
Example #6
0
 public void TestFaceUpdate()
 {
     using (var scope = new TransactionScope())
     {
         using (var context = new BFDbContext())
         {
             try
             {
                 context.Database.Log = Console.WriteLine;
                 //var face = context.Face.AsNoTracking().FirstOrDefault();
                 var face = context.Face.FirstOrDefault();
                 face.Name = "修改后的名字";
                 //context.Face.Attach(face);
                 if (!context.ChangeTracker.HasChanges())
                 {
                     Console.WriteLine("实体对象没有发生变化!");
                 }
                 else
                 {
                     Console.WriteLine("实体对象发生了变化!");
                     context.Entry(face).State = EntityState.Modified;
                     int c = context.SaveChanges();
                     Assert.Greater(c, 0);
                     Console.WriteLine("修改数据的影响行数:{0}", c);
                 }
                 scope.Complete();
             }
             catch (DbUpdateConcurrencyException ex)
             {
                 var entry = ex.Entries.Single();
                 entry.OriginalValues.SetValues(entry.GetDatabaseValues());
                 context.SaveChanges();
             }
         }
     }
 }
Example #7
0
        public static void Test()
        {
            using (var scope = new TransactionScope())
            {
                Database.SetInitializer <BFDbContext>(null);
                using (var context = new BFDbContext())
                {
                    context.Database.Log = Console.WriteLine;
                    try
                    {
                        FaceType faceType = new FaceType()
                        {
                            Id = 1, Name = "FaceType1"
                        };
                        Photo photo = new Photo()
                        {
                            FaceId = 1, Path = "http://localhost:8088/testPDF/20190410170225.pdf"
                        };
                        Face face = new Face()
                        {
                            Id       = 1,
                            Name     = "Face1",
                            TypeId   = 1,
                            FaceType = faceType,
                            Photo    = photo
                        };

                        //context.Entry(faceType).State = EntityState.Added;
                        context.Entry(face).State = EntityState.Added;
                        context.SaveChanges();

                        //foreach (var v in context.FaceType)
                        //{
                        //    Console.WriteLine(string.Format("FaceType:{0},{1}", v.Id, v.Name));
                        //}

                        //foreach (var v in context.Face)
                        //{
                        //    Console.WriteLine(string.Format("ID:{0},Name:{1},PhotoPath:{2}", v.Id, v.Name, v.Photo.Path));
                        //}

                        //foreach (var v in context.Face)
                        //{
                        //    v.FaceType = context.FaceType.Find(v.TypeId);
                        //    Console.WriteLine(string.Format("ID:{0},Name:{1},PhotoPath:{2},FaceType:{3}", v.Id, v.Name, v.Photo.Path, v.FaceType.Name));
                        //}

                        foreach (var v in context.Face)
                        {
                            Console.WriteLine(string.Format("ID:{0},Name:{1},PhotoPath:{2},FaceType:{3},TYPEID:{4}", v.Id, v.Name, v.Photo.Path, v.FaceType.Name, v.TypeId));
                        }

                        scope.Complete();
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.Message);
                    }
                }
            }
        }
Example #8
0
        public static void Test()
        {
            using (var scope = new TransactionScope())
            {
                Database.SetInitializer <BFDbContext>(null);
                using (var context = new BFDbContext())
                {
                    context.Database.Log = Console.WriteLine;
                    try
                    {
                        Nationality cn = new Nationality()
                        {
                            Id = 1, Name = "中国"
                        };
                        Nationality us = new Nationality()
                        {
                            Id = 2, Name = "美国"
                        };

                        //注意这里要给Person类的导航属性赋值,NationalityId属性不用赋值
                        Person cnP = new Person()
                        {
                            Id = 1, Name = "徐齐", Age = 86, Nationality = cn
                        };
                        Person usP = new Person()
                        {
                            Id = 2, Name = "王穗", Age = 86, Nationality = us
                        };

                        context.Entry(cnP).State = EntityState.Added;
                        context.Entry(usP).State = EntityState.Added;
                        context.SaveChanges();

                        Console.WriteLine(string.Format("人的数量:{0}", context.Person.Count()));
                        var personLst = context.Person.Local;
                        foreach (var p in personLst)
                        {
                            Console.WriteLine(string.Format("ID:{0},Name:{1},Nationality:{2}", p.Id, p.Name, p.Nationality.Name));
                        }
                        Console.WriteLine(string.Format("国家的数量:{0}", context.Nationality.Count()));
                        var nationLst = context.Nationality.Local;
                        foreach (var n in nationLst)
                        {
                            foreach (var p in n.Persons)
                            {
                                Console.WriteLine(string.Format("ID:{0},Name:{1},Person:{2}", n.Id, n.Name, p.Name));
                            }
                        }

                        //Include只能用于导航属性的联合查询,他的参数是导航属性的名称
                        var a = context.Person.Where(r => r.Id == 1).Include("Nationality").SingleOrDefault();
                        //context.Entry(a).Property(p => p.Name).IsModified = true;
                        Console.WriteLine(string.Format("根据导航属性查询的国家:", a.Id, a.Name));
                        var b = context.Nationality.First().Persons.ToList();


                        scope.Complete();
                    }
                    catch (Exception e) {
                        Console.WriteLine(e.Message);
                    }
                }
            }
        }