当前位置: 首页>>代码示例>>C#>>正文


C# EFRepository.Delete方法代码示例

本文整理汇总了C#中EFRepository.Delete方法的典型用法代码示例。如果您正苦于以下问题:C# EFRepository.Delete方法的具体用法?C# EFRepository.Delete怎么用?C# EFRepository.Delete使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在EFRepository的用法示例。


在下文中一共展示了EFRepository.Delete方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Delete_Deletes_Record

        public void Delete_Deletes_Record()
        {
            var newCustomer = new Customer
            {
                FirstName = ("John_DELETE_ME_" + DateTime.Now),
                LastName = ("Doe_DELETE_ME_" + DateTime.Now),
                StreetAddress1 = "This record was inserted for deletion",
                City = "Fictional city",
                State = "LA",
                ZipCode = "12345"
            };

            //Re-usable query to query for the matching record.
            var queryForCustomer = new Func<EFRepository<Customer>, Customer>
                (
                x => (from cust in x
                      where cust.FirstName == newCustomer.FirstName && cust.LastName == newCustomer.LastName
                      select cust).FirstOrDefault()
                );

            using (var scope = new UnitOfWorkScope())
            {
                var customerRepository = new EFRepository<Customer>();
                var recordCheckResult = queryForCustomer(customerRepository);
                Assert.That(recordCheckResult, Is.Null);

                customerRepository.Add(newCustomer);
                scope.Commit();
            }

            //Retrieve the record for deletion.
            using (var scope = new UnitOfWorkScope())
            {
                var customerRepository = new EFRepository<Customer>();
                var customerToDelete = queryForCustomer(customerRepository);
                Assert.That(customerToDelete, Is.Not.Null);
                customerRepository.Delete(customerToDelete);
                scope.Commit();
            }

            //Ensure customer record is deleted.
            using (new UnitOfWorkScope())
            {
                var customerRepository = new EFRepository<Customer>();
                var recordCheckResult = queryForCustomer(customerRepository);
                Assert.That(recordCheckResult, Is.Null);
            }
        }
开发者ID:tmchan,项目名称:ncommon,代码行数:48,代码来源:EFRepositoryTests.cs

示例2: GetCountDeleteIsOK

 public void GetCountDeleteIsOK()
 {
     var context = new CarContext();
     var testContext = new EFRepository<Car,int>(context);
     var all = context.Car.ToList();
     all.ForEach(r => testContext.Delete(r));
     testContext.Save();
     Assert.AreEqual(context.Car.ToList().Count, 0);
     context.Car.Add(new Car()
     {
         CarName = "Ford",
         CarPrice = 100000
     });
     testContext.Save();
     var cars = context.Car.ToList();
     Assert.AreEqual(testContext.Count(), 1);
     Assert.AreEqual(testContext.Get(r => r.CarPrice == 100000).CarName, "Ford");
 }
开发者ID:Indifer,项目名称:Raven.EntityFramework.Repository,代码行数:18,代码来源:UnitTest2.cs

示例3: Can_delete

        public void Can_delete()
        {
            Customer customer = null;
            var testData = new EFTestData(Context);
            testData.Batch(x => customer = x.CreateCustomer());
            using (var scope = new UnitOfWorkScope())
            {
                var repository = new EFRepository<Customer>();
                var savedCustomer = repository.First(x => x.CustomerID == customer.CustomerID);
                repository.Delete(savedCustomer);
                scope.Commit();
            }

            using (var scope = new UnitOfWorkScope())
            {
                var repository = new EFRepository<Customer>();
                Assert.That(repository.FirstOrDefault(x => x.CustomerID == customer.CustomerID), Is.Null);
                scope.Commit();
            }
        }
开发者ID:jordanyaker,项目名称:ncommon,代码行数:20,代码来源:EFRepositoryQueryTests.cs

示例4: Save_New_Customer_Saves_Customer_When_UnitOfWork_Is_Committed

        public void Save_New_Customer_Saves_Customer_When_UnitOfWork_Is_Committed()
        {
            var rnd = new Random();
            var newCustomer = new Customer
            {
                FirstName = ("John_" + rnd.Next(0, 30000)),
                LastName = ("Doe_" + rnd.Next(0, 30000)),
                StreetAddress1 = "This record was inserted via a test",
                City = "Fictional city",
                State = "LA",
                ZipCode = "12345"
            };

            var queryForCustomer = new Func<EFRepository<Customer>, Customer>
                (
                x => (from cust in x
                      where cust.FirstName == newCustomer.FirstName && cust.LastName == newCustomer.LastName
                      select cust).FirstOrDefault()
                );

            using (var scope = new UnitOfWorkScope())
            {
                var customerRepository = new EFRepository<Customer>();
                var recordCheckResult = queryForCustomer(customerRepository);
                Assert.That(recordCheckResult, Is.Null);

                customerRepository.Add(newCustomer);
                scope.Commit();
            }

            //Starting a completely new unit of work and repository to check for existance.
            using (var scope = new UnitOfWorkScope())
            {
                var customerRepository = new EFRepository<Customer>();
                var recordCheckResult = queryForCustomer(customerRepository);
                Assert.That(recordCheckResult, Is.Not.Null);
                Assert.That(recordCheckResult.FirstName, Is.EqualTo(newCustomer.FirstName));
                Assert.That(recordCheckResult.LastName, Is.EqualTo(newCustomer.LastName));
                customerRepository.Delete(recordCheckResult); //Deleting record after verification.
                scope.Commit();
            }
        }
开发者ID:tmchan,项目名称:ncommon,代码行数:42,代码来源:EFRepositoryTests.cs

示例5: Delete_NonExistingNews_ShouldThrow

        public void Delete_NonExistingNews_ShouldThrow()
        {
            // Arrange
            this.CleanUpDatabase();
            var listWithNews = new List<News>();
            var repo = new EFRepository<News>(this.dbcontext);

            // Act
            var newsInDb = repo.All().ToArray();
            repo.Delete(newsInDb[0]); // Non existing item at index 0.
        }
开发者ID:WS-and-Cloud,项目名称:News-UnitTests,代码行数:11,代码来源:NewsRepositoryCrud.cs

示例6: Delete_ExistingNews_ShouldDeleteTheNew

        public void Delete_ExistingNews_ShouldDeleteTheNew()
        {
            // Arrange
            this.CleanUpDatabase();
            var listWithNews = new List<News>();
            var repo = new EFRepository<News>(this.dbcontext);
            var new1 = new News() { Content = "Content of new", Title = "Title 1", PublishedAt = DateTime.Now };

            // Act
            repo.Add(new1);
            repo.SaveChanges();
            var newsInDb = repo.All().ToArray();
            repo.Delete(newsInDb[0]);
            repo.SaveChanges();

            // Assert
            Assert.AreEqual(0, repo.All().ToList().Count());
        }
开发者ID:WS-and-Cloud,项目名称:News-UnitTests,代码行数:18,代码来源:NewsRepositoryCrud.cs

示例7: UpdateIsOK

        public void UpdateIsOK()
        {
            var context = new CarContext();
            var testContext = new EFRepository<Car,int>(context);
            var all = context.Car.ToList();
            all.ForEach(r => testContext.Delete(r));
            testContext.Save();
            var car = new Car()
            {
                CarName = "Ford",
                CarPrice = 100000
            };
            testContext.Insert(car);
            testContext.Save();

            car.CarName = "Ford1";
            car.CarPrice = 100000 - 1;
            testContext.Update(car);
            testContext.Save();

            var cars = context.Car.ToList();
            Assert.AreEqual(cars.Count, 1);
            Assert.AreEqual(cars.FirstOrDefault().CarName, "Ford1");
            Assert.AreEqual(cars.FirstOrDefault().CarPrice, 100000 - 1);
        }
开发者ID:Indifer,项目名称:Raven.EntityFramework.Repository,代码行数:25,代码来源:UnitTest2.cs

示例8: MyTestMethod

        public void MyTestMethod()
        {
            var context = new CarContext();
            var testContext = new EFRepository<Car, int>(context);
            context.Car.ToList().ForEach(r => testContext.Delete(r));
            testContext.Save();
            var car = new Car()
            {
                CarName = "Ford",
                CarPrice = 100000
            };
            var car1 = new Car()
            {
                CarName = "Toyota",
                CarPrice = 100000
            };
            testContext.Insert(car);
            testContext.Insert(car1);
            testContext.Save();

            //var cars = testContext.GetList();
            //Assert.AreEqual(cars.Count, 1);
            //Assert.AreEqual(cars.FirstOrDefault().CarName, "Ford");
            //Assert.AreEqual(cars.FirstOrDefault().CarPrice, 100000);
        }
开发者ID:Indifer,项目名称:Raven.EntityFramework.Repository,代码行数:25,代码来源:UnitTest2.cs

示例9: GetAllOrderByIsOK

 public void GetAllOrderByIsOK()
 {
     var context = new CarContext();
     var testContext = new EFRepository<Car,int>(context);
     context.Car.ToList().ForEach(r => testContext.Delete(r));
     testContext.Save();
     var car = new Car()
     {
         CarName = "Ford",
         CarPrice = 100000
     };
     var car1 = new Car()
     {
         CarName = "Toyota",
         CarPrice = 99999
     };
     testContext.Insert(car);
     testContext.Insert(car1);
     testContext.Save();
     var cars = testContext.GetList(r => r.CarPrice == 100000, null, "").ToList();
     Assert.AreEqual(cars.Count, 1);
     Assert.AreEqual(cars.FirstOrDefault().CarName, "Ford");
     Assert.AreEqual(cars.FirstOrDefault().CarPrice, 100000);
 }
开发者ID:Indifer,项目名称:Raven.EntityFramework.Repository,代码行数:24,代码来源:UnitTest2.cs

示例10: Delete1IsOK

 public void Delete1IsOK()
 {
     var context = new CarContext();
     var testContext = new EFRepository<Car,int>(context);
     var all = context.Car.ToList();
     all.ForEach(r => testContext.Delete(r));
     testContext.Save();
     var car = new Car()
     {
         CarName = "Ford",
         CarPrice = 100000
     };
     testContext.Insert(car);
     testContext.Save();
     testContext.Delete(r => r.ID == car.ID);
     testContext.Save();
     var cars = context.Car.ToList();
     Assert.AreEqual(cars.Count, 0);
 }
开发者ID:Indifer,项目名称:Raven.EntityFramework.Repository,代码行数:19,代码来源:UnitTest2.cs

示例11: Update1IsOK

        public void Update1IsOK()
        {
            var context = new CarContext();
            var testContext = new EFRepository<Car,int>(context);
            var all = context.Car.ToList();
            all.ForEach(r => testContext.Delete(r));
            testContext.Save();
            var car = new Car()
            {
                CarName = "Ford",
                CarPrice = 100000
            };
            testContext.Insert(car);
            testContext.Save();
            car.CarPrice = 99999;
            car.CarName = "Ford1";
            testContext.Update(car, x => new { x.CarPrice });
            testContext.Save();
            var cars = testContext.GetDbSet().AsNoTracking().ToList();//OK


            var cars1 = testContext.GetDbSet().ToList();//error            
            Assert.AreEqual(cars.Count, 1);
            Assert.AreEqual(cars.FirstOrDefault().CarName, "Ford");
            Assert.AreEqual(cars.FirstOrDefault().CarPrice, 100000 - 1);
        }
开发者ID:Indifer,项目名称:Raven.EntityFramework.Repository,代码行数:26,代码来源:UnitTest2.cs

示例12: Can_delete

        public void Can_delete()
        {
            var customer = new Customer
            {
                FirstName = "John",
                LastName = "Doe",
            };
            using (var scope = new UnitOfWorkScope())
            {
                new EFRepository<Customer>().Add(customer);
                scope.Commit();
            }
            Assert.That(customer.CustomerID, Is.GreaterThan(0));
            using (var scope = new UnitOfWorkScope())
            {
                var repository = new EFRepository<Customer>();
                var savedCustomer = repository.Where(x => x.CustomerID == customer.CustomerID).First();
                repository.Delete(savedCustomer);
                scope.Commit();
            }

            //Making sure customer is deleted
            using (var testData = new EFTestData(OrdersContextProvider()))
            {
                Customer savedCustomer = null;
                testData.Batch(x => savedCustomer = x.GetCustomerById(customer.CustomerID));
                Assert.That(savedCustomer, Is.Null);
            }
        }
开发者ID:jordanyaker,项目名称:ncommon,代码行数:29,代码来源:EFRepositoryQueryTests.cs


注:本文中的EFRepository.Delete方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。