Ejemplo n.º 1
0
 public void DeleteTest()
 {
     using (var dbContext = new AdventureWorks2014DbContext())
     {
         Address _fined = dbContext.Find <Address>(22);
         dbContext.Delete <Address>(_fined);
         _fined = dbContext.Find <Address>(22);
         Assert.IsNull(_fined);
     }
 }
Ejemplo n.º 2
0
 public void UpdateTest()
 {
     using (var dbContext = new AdventureWorks2014DbContext())
     {
         Address _actual = dbContext.Find <Address>(32529);
         string  _expect = _actual.AddressLine1 + DateTime.Now.ToString("yyyyMMddHHmmss");
         _actual.AddressLine1 = _expect;
         dbContext.Update <Address>(_actual);
         _actual = dbContext.Find <Address>(32529);
         Assert.AreEqual(_actual.AddressLine1, _expect);
     }
 }
Ejemplo n.º 3
0
 public void FindTest()
 {
     using (var dbContext = new AdventureWorks2014DbContext())
     {
         Address _actual = dbContext.Find <Address>(1);
         Assert.IsNotNull(_actual);
         Assert.AreEqual(_actual.ID, 1);
     }
 }
Ejemplo n.º 4
0
 public void DeleteTest()
 {
     using (AdventureWorks2014DbContext dbContext = new AdventureWorks2014DbContext())
     {
         using (System.Data.Entity.DbContextTransaction tran = dbContext.Database.BeginTransaction())
         {
             try
             {
                 Address _fined = dbContext.Find <Address>(22);
                 dbContext.Delete <Address>(_fined);
                 _fined = dbContext.Find <Address>(22);
                 Assert.IsNull(_fined);
                 dbContext.SaveChanges();
                 tran.Commit();
             }
             catch (Exception)
             {
                 tran.Rollback();
             }
         }
     }
 }
Ejemplo n.º 5
0
        public void CommitTest()
        {
            using (var dbContext = new AdventureWorks2014DbContext())
            {
                dbContext.BeginTransaction();

                try
                {
                    Address _item = new Address();
                    _item.PostalCode      = "78019";
                    _item.AddressLine1    = "Tianxin";
                    _item.City            = "Xiangtan";
                    _item.StateProvinceID = 78;
                    _item.PostalCode      = "72228";
                    _item.rowguid         = Guid.NewGuid();
                    dbContext.Insert <Address>(_item);
                    Address _fined = dbContext.Find <Address>(22);
                    dbContext.Delete <Address>(_fined);
                    dbContext.Commit();
                }

                catch (Exception)
                {
                    dbContext.Rollback();
                }

                finally
                {
                    Address _xiangtan = dbContext.FindAll <Address>(ent => ent.City == "Xiangtan").FirstOrDefault();
                    Assert.IsNull(_xiangtan);
                }
            }

            using (var dbContext = new AdventureWorks2014DbContext())
            {
                dbContext.BeginTransaction();

                try
                {
                    dbContext.BeginTransaction();
                    Address _jiaxing = new Address();
                    _jiaxing.PostalCode      = "78019";
                    _jiaxing.AddressLine1    = "Tianxin";
                    _jiaxing.City            = "Jiaxing";
                    _jiaxing.StateProvinceID = 78;
                    _jiaxing.PostalCode      = "72228";
                    _jiaxing.rowguid         = Guid.NewGuid();
                    dbContext.Insert <Address>(_jiaxing);
                    Address _hangzhou = new Address();
                    _hangzhou.PostalCode      = "78019";
                    _hangzhou.AddressLine1    = "Tianxin";
                    _hangzhou.City            = "HangZhou";
                    _hangzhou.StateProvinceID = 78;
                    _hangzhou.PostalCode      = "72228";
                    _hangzhou.rowguid         = Guid.NewGuid();
                    dbContext.Insert <Address>(_hangzhou);
                    dbContext.Commit();
                }

                catch (Exception)
                {
                    dbContext.Rollback();
                }

                finally
                {
                    Address _hangZhou = dbContext.FindAll <Address>(ent => ent.City == "HangZhou").FirstOrDefault();
                    Assert.IsNotNull(_hangZhou);
                    Address _jiaxing = dbContext.FindAll <Address>(ent => ent.City == "Jiaxing").FirstOrDefault();
                    Assert.IsNotNull(_jiaxing);
                }
            }
        }