本文整理汇总了C#中EFRepository.Update方法的典型用法代码示例。如果您正苦于以下问题:C# EFRepository.Update方法的具体用法?C# EFRepository.Update怎么用?C# EFRepository.Update使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类EFRepository
的用法示例。
在下文中一共展示了EFRepository.Update方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: 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);
}
示例2: UpdateNews_WithIncorrectData_ShouldThrow
public void UpdateNews_WithIncorrectData_ShouldThrow()
{
// 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();
string newTitle = null;
newsInDb[0].Title = newTitle;
repo.Update(newsInDb[0]);
repo.SaveChanges();
var latestNewsInDb = repo.All().ToArray();
// Assert
Assert.AreEqual(1, newsInDb.Count());
Assert.AreEqual(new1.Title, latestNewsInDb[0].Title);
Assert.AreEqual(new1.Content, latestNewsInDb[0].Content);
}
示例3: 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);
}