本文整理汇总了C#中Repository.SaveChanges方法的典型用法代码示例。如果您正苦于以下问题:C# Repository.SaveChanges方法的具体用法?C# Repository.SaveChanges怎么用?C# Repository.SaveChanges使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Repository
的用法示例。
在下文中一共展示了Repository.SaveChanges方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: AddNewsWhenNewsIsAddedToDbShouldReturnNews
public void AddNewsWhenNewsIsAddedToDbShouldReturnNews()
{
//Arange -> prepare the objects
var repo = new Repository<Models.News>(new NewsContext());
var news = new Models.News()
{
Id = 1,
Title = "title 1",
Content = "content 1",
PublishedData = DateTime.Now
};
//Act -> perform some logic
repo.Add(news);
repo.SaveChanges();
//Assert -> validate the results
var newsFromDb = repo.GetById(news.Id);
Assert.IsNotNull(newsFromDb);
Assert.AreEqual(news.Title, newsFromDb.Title);
Assert.AreEqual(news.Content, newsFromDb.Content);
Assert.AreEqual(news.PublishedData, newsFromDb.PublishedData);
Assert.IsTrue(newsFromDb.Id != 0);
}
示例2: SRVCUpdateLogins
private IEnumerable<EmployeeLogin> SRVCUpdateLogins(Helpers.Log.SessionInfo logSession, Repository.Logic.Repository rep,
long employeeId, IEnumerable<string> addLogins, IEnumerable<string> removeLogins)
{
#pragma warning disable 618
logSession.Add($"Try to get employee with id = {employeeId}");
var emp = rep.Get<Repository.Model.Employee>(e => e.EmployeeId == employeeId, false, new string[] { "Logins" }).FirstOrDefault();
if (emp == null)
throw new Exception(string.Format(Properties.Resources.STUFFINGSERVICE_EmployeeNotFound, employeeId));
var existedLogins = emp.Logins.Select(r => r.DomainLogin);
#region Add logins
if (addLogins != null && addLogins.Any())
{
logSession.Add($"Add logins...");
var addLoginsUpper = addLogins
.Except(existedLogins)
.ToArray()
.Select(r => rep.New<Repository.Model.EmployeeLogin>((er) =>
{
er.EmployeeLoginId = emp.EmployeeId;
er.DomainLogin = r;
}))
.ToArray();
logSession.Add($"Add this logins {addLoginsUpper.Concat(r => r.DomainLogin, ",")} for employee id = {employeeId}");
foreach (var r in addLoginsUpper)
emp.Logins.Add(r);
rep.AddRange(addLoginsUpper, saveAfterInsert: false);
}
#endregion
#region Remove rights
if (removeLogins != null && removeLogins.Any())
{
logSession.Add($"Remove logins...");
var removeLoginsUpper = removeLogins
.Intersect(existedLogins)
.ToArray()
.Join(emp.Logins, r => r, er => er.DomainLogin.ToUpper(), (r, er) => er)
.ToArray();
logSession.Add($"Remove this logins {removeLoginsUpper.Concat(r => r.DomainLogin, ",")} for employee id = {employeeId}");
foreach (var r in removeLoginsUpper)
emp.Logins.Remove(r);
rep.RemoveRange(removeLoginsUpper, saveAfterRemove: false);
}
#endregion
rep.SaveChanges();
return emp.Logins.Select(er => AutoMapper.Mapper.Map<EmployeeLogin>(er));
#pragma warning restore 618
}
示例3: Create
//redundant
//public List<Accounts>AccountsList { get; set; }
public void Create(Tweets persistObject)
{
using (Repository<Tweets> DataAccessHelper = new Repository<Tweets>())
{
DataAccessHelper.Add(persistObject);
DataAccessHelper.SaveChanges();
}
}
示例4: AddNewProduct
public void AddNewProduct()
{
using (var dataAccessHelper = new Repository<Product>())
{
dataAccessHelper.Add(this);
dataAccessHelper.SaveChanges();
}
}
示例5: SaveObject
public void SaveObject(Tweets tweet)
{
using (Repository<Tweets> DataAccessHelper = new Repository<Tweets>())
{
DataAccessHelper.Add(tweet);
DataAccessHelper.SaveChanges();
}
}
示例6: BatchInsertAndRemove
//===============================================================
public static void BatchInsertAndRemove(Repository<TestClass> repo)
{
var testObjects = Enumerable.Range(0, 5).Select(x => new TestClass(x.ToString(), x.ToString())).ToList();
repo.RemoveAll(repo.Items);
repo.SaveChanges();
Assert.IsTrue(!repo.Items.Any());
repo.Insert(testObjects);
repo.SaveChanges();
Assert.IsTrue(repo.Items.Any());
Assert.IsTrue(repo.Items.ToList().All(x => testObjects.Exists(y => y.ID == x.ID)));
// Remove all batch
repo.RemoveAll();
repo.SaveChanges();
Assert.IsTrue(!repo.Items.Any());
// Remove all without saving in between
repo.Insert(testObjects);
repo.RemoveAll();
repo.SaveChanges();
Assert.IsTrue(!repo.Items.Any());
// Remove all with inserts before and after
repo.Insert(testObjects);
repo.RemoveAll();
repo.Insert(testObjects);
repo.RemoveAll();
repo.SaveChanges();
Assert.IsTrue(!repo.Items.Any());
repo.Insert(testObjects);
repo.SaveChanges();
// Remove all by key
repo.RemoveAllByKey(repo.Items.ToList().Select(x => new object[] { x.ID }));
repo.SaveChanges();
Assert.IsTrue(!repo.Items.Any());
// Remove all by object
repo.Insert(testObjects);
repo.SaveChanges();
repo.RemoveAll(testObjects);
repo.SaveChanges();
Assert.IsTrue(!repo.Items.Any());
}
示例7: Here
public ActionResult Here(int locationId)
{
// Get the data
IRepository repository = new Repository(new CheckInDatabaseContext());
var location = repository.GetById<Location>(locationId);
if (location == null)
{
return new HttpNotFoundResult();
}
var username = HttpContext.User.Identity.Name;
var user = repository.Query<ApplicationUser>().SingleOrDefault(u => u.UserName == username);
if (user == null)
{
return new HttpNotFoundResult();
}
// make a new check in
var checkIn = new CheckIn();
checkIn.User = user;
checkIn.Location = location;
checkIn.Time = DateTime.Now;
repository.Insert(checkIn);
// check to see if this user meets any achievements
var allCheckins = repository.Query<CheckIn>().Where(c => c.User.Id == user.Id);
var allAchievements = repository.Query<Achievement>();
var allLocationIds = repository.Query<Location>().Select(l => l.Id);
// two in one day?
if (!allAchievements.Any(a => a.Type == AchievementType.TwoInOneDay) && allCheckins.Count(c => EntityFunctions.TruncateTime(c.Time) == DateTime.Today) > 2)
{
var twoInOneDay = new Achievement { Type = AchievementType.TwoInOneDay, User = user, TimeAwarded = DateTime.Now };
repository.Insert(twoInOneDay);
}
// all locations?
var hasAll = false;
foreach (var testLocationId in allLocationIds)
{
hasAll = hasAll || allCheckins.Any(c => c.Location.Id == testLocationId);
}
if (!allAchievements.Any(a => a.Type == AchievementType.AllLocations) && hasAll)
{
var allLocations = new Achievement { Type = AchievementType.AllLocations, User = user, TimeAwarded = DateTime.Now };
repository.Insert(allLocations);
}
// some day we'll have hundreds of achievements!
repository.SaveChanges();
return RedirectToAction("Index");
}
示例8: AddNews_InValidNewsAddedToDb_ShouldThrowException
public void AddNews_InValidNewsAddedToDb_ShouldThrowException()
{
// Arrange -> prapare the objects
var newInvalidNews = new News { Title = "Invalid", Content = "Something", PublishDate = DateTime.Now };
IRepository<News> repository = new Repository<News>();
// Act -> perform some logic
repository.Add(newInvalidNews);
repository.SaveChanges();
}
示例9: DeleteFile
public ActionResult DeleteFile(Guid id)
{
using (Repository r = new Repository())
{
FileUploadEntity f = r.Files.FirstOrDefault(x => x.ID == id);
if (f != null)
{
r.Files.Remove(f);
r.SaveChanges();
}
}
return RedirectToAction("Gallery");
}
示例10: AddNewsWhenNewsIsInvalidShouldThrowException
public void AddNewsWhenNewsIsInvalidShouldThrowException()
{
//Arrange -> prepare the objects
var repo = new Repository<Models.News>(new NewsContext());
var invalidNews = new Models.News()
{
Content = null
};
//Act -> perform some logic
repo.Add(invalidNews);
repo.SaveChanges();
//Assert -> expect an exception
}
示例11: AddNews_ValidNewsAddedToDb_ShouldReturnNews
public void AddNews_ValidNewsAddedToDb_ShouldReturnNews()
{
// Arrange -> prapare the objects
var newValidNews = new News { Title = "Valid news added", Content = "Something", PublishDate = DateTime.Now };
IRepository<News> repository = new Repository<News>();
// Act -> perform some logic
repository.Add(newValidNews);
repository.SaveChanges();
// Assert -> validate the results
var newsFromDb = repository.Find(newValidNews.Id);
Assert.IsNotNull(newsFromDb);
Assert.AreEqual(newValidNews.Title, newsFromDb.Title);
Assert.AreEqual(newValidNews.Content, newsFromDb.Content);
Assert.AreEqual(newValidNews.PublishDate, newsFromDb.PublishDate);
Assert.IsTrue(newsFromDb.Id != 0);
}
示例12: ModifyNews_ValidNews_ShouldModifyNews
public void ModifyNews_ValidNews_ShouldModifyNews()
{
// Arrange -> prapare the objects
IRepository<News> repository = new Repository<News>();
ClearDatabase(repository);
SeedNews(repository);
var newsFromDb = repository.All().FirstOrDefault();
string title = "Modified Title";
var content = "Modified Content";
var dateTimeNow = DateTime.Now;
// Act -> perform some logic
newsFromDb.Title = title;
newsFromDb.Content = content;
newsFromDb.PublishDate = dateTimeNow;
repository.SaveChanges();
// Assert -> validate the results
var modifiedNewsFromDb = repository.Find(newsFromDb.Id);
Assert.IsNotNull(modifiedNewsFromDb);
Assert.AreEqual(content, modifiedNewsFromDb.Content);
Assert.AreEqual(title, modifiedNewsFromDb.Title);
Assert.AreEqual(dateTimeNow.ToString(), modifiedNewsFromDb.PublishDate.ToString());
}
示例13: DeleteNews_NonExistingNews_ShouldThrowException
public void DeleteNews_NonExistingNews_ShouldThrowException()
{
// Arrange -> prapare the objects
IRepository<News> repository = new Repository<News>();
ClearDatabase(repository);
SeedNews(repository);
var newsFromDb = repository.Find(1000000);
// Act -> perform some logic
repository.Delete(newsFromDb);
repository.SaveChanges();
// Assert -> validate the results
}
示例14: StoreHours
public ActionResult StoreHours(StoreHoursVM vm)
{
using (var r = new Repository())
{
var entity = r.StoreHours.First();
entity.Line1 = vm.Line1;
entity.Line2 = vm.Line2;
entity.Line3 = vm.Line3;
r.SaveChanges();
}
return View(vm);
}
示例15: ChangeTracking
//===============================================================
public static void ChangeTracking(Repository<TestClass> repo)
{
var obj = new TestClass("myKey", "myValue");
repo.Insert(obj);
repo.SaveChanges();
var storedObj = repo.Find(obj.ID);
storedObj.Object.StringValue = "newValue";
repo.SaveChanges();
storedObj = repo.Find(obj.ID);
Assert.AreEqual("newValue", storedObj.Object.StringValue);
// Test Update function
repo.Update(new { StringValue = "newValue2" }, storedObj.Object.ID);
repo.SaveChanges();
storedObj = repo.Find(obj.ID);
Assert.AreEqual("newValue2", storedObj.Object.StringValue);
repo.RemoveAll();
repo.SaveChanges();
// Test changing enumerated objects
var objects = Enumerable.Range(0, 10).Select(x => new TestClass(x.ToString(), x.ToString()));
repo.Insert(objects);
repo.SaveChanges();
foreach (var item in repo.Items)
Assert.AreEqual(item.ID, item.StringValue);
foreach (var item in repo.Items)
item.StringValue = ((int.Parse(item.StringValue)) + 1).ToString();
repo.SaveChanges();
foreach (var item in repo.Items)
Assert.AreNotEqual(item.ID, item.StringValue);
repo.RemoveAll();
repo.SaveChanges();
}