本文整理汇总了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);
}
}
示例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");
}
示例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();
}
}
示例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();
}
}
示例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.
}
示例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());
}
示例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);
}
示例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);
}
示例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);
}
示例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);
}
示例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);
}
示例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);
}
}