public void Initialize() { if (context.Books.Count() == 0) { context.Books.AddRange( new List <Book>() { new Book() { Title = "Book 1", Year = 1996 }, new Book() { Title = "Book 2", Year = 1993 }, new Book() { Title = "Book 3", Year = 1999 }, new Book() { Title = "Book 4", Year = 1991 }, new Book() { Title = "Book 5", Year = 1990 }, }); context.SaveChanges(); } }
public void CreateOperation(Operation operation) { if (operation != null) { db.Operation.Add(operation); db.SaveChanges(); } }
public void CreateGroup(OperationGroup group) { if (group != null) { db.OperationGroup.Add(group); db.SaveChanges(); } }
public void TestInsert() { books.Insert(new Book { Title = "My new book" }); context.SaveChanges(); Assert.Equal(3, books.Get().Count()); }
public ResultModel AddOrder(OrderDTO order) { ResultModel result = new ResultModel(); BookDTO book = new BookDTO { AuthorId = order.Author.Id, Title = order.Book.Title }; int bookId = AddBook(book); if (bookId > 0) { using (var transaction = _context.Database.BeginTransaction()) { try { OrderLists item = new OrderLists { Book = bookId, Cost = order.Cost, Supply = order.Supply.Id }; OrderLists item1 = (OrderLists)order; item.Book = bookId; _context.OrderLists.Add(item); Supplies supply = _context.Supplies.FirstOrDefault(c => c.Id == order.Supply.Id); double oldSumm = supply.Summ.HasValue ? supply.Summ.Value : 0; supply.Summ = oldSumm + order.Cost; _context.Entry(supply).State = EntityState.Modified; _context.SaveChanges(); transaction.Commit(); } catch (Exception ex) { result.Code = OperationStatusEnum.UnexpectedError; result.Message = ex.StackTrace; transaction.Rollback(); } } } else { result.Code = OperationStatusEnum.UnexpectedError; result.Message = "Ошибка при добавлении книги"; } return(result); }
public ActionResult Registration(User user) { if (user != null) { db.Users.Add(user); db.SaveChanges(); Session["Nickname"] = user.Nickname; Session["Id"] = db.Users.Where(u => u.Nickname == user.Nickname).FirstOrDefault().UserId; } return(Redirect("/Home/Index")); }
public void InsertPublisher() { Publisher p = new Publisher { Name = "New publisher" }; publishers.Insert(p); int N = context.SaveChanges(); Assert.Equal(1, N); Assert.Equal(3, p.Id); }
public void CreateUser(RegisterViewModel user) { OperationGroup group = db.OperationGroup.Where(m => m.Name == "Users").FirstOrDefault(); if (user != null) { User newUser = new User(); newUser.Id = user.Id; newUser.Username = user.Username; newUser.Salt = CreateSalt(); newUser.Password = user.Password; newUser.IsActive = true; newUser.data = DateTime.UtcNow.AddYears(1); newUser.Operations = new List <Operation>(); newUser.OperationGroups = new List <OperationGroup>(); newUser.OperationGroups.Add(group); foreach (Operation oper in group.Operations) { if (!newUser.Operations.Contains(oper)) { newUser.Operations.Add(oper); } } db.User.Add(newUser); db.SaveChanges(); } }
private static void Main(string[] args) { using (LibContext contex = new LibContext()) { Publisher p = new Publisher(); Console.WriteLine("Publisher name: "); p.Name = Console.ReadLine(); Console.WriteLine("Address: "); p.Road = Console.ReadLine(); Console.WriteLine("Zip code: "); p.ZipCode = Console.ReadLine(); Console.WriteLine("City: "); p.City = Console.ReadLine(); Console.WriteLine("Country: "); p.Country = Console.ReadLine(); contex.Publishers.Add(p); contex.SaveChanges(); Book b = new Book(); Console.WriteLine("Book title: "); b.Title = Console.ReadLine(); Console.WriteLine("Book cover: "); b.Image = Console.ReadLine(); Console.WriteLine("Number of pages: "); b.Pages = int.Parse(Console.ReadLine()); Console.WriteLine("Book price: "); b.Price = decimal.Parse(Console.ReadLine()); Console.Write("Publisher: "); string pubName = Console.ReadLine(); Publisher pub = contex.Publishers.FirstOrDefault(x => x.Name == pubName); b.Publisher = pub; contex.Books.Add(b); int n = contex.SaveChanges(); Console.WriteLine($"{ n } transactions commited."); } }
public void Add(object sender, RoutedEventArgs e) { EditRecord recordEdit = new EditRecord(new Record()); recordEdit.record.Reader = readerGrid.SelectedItem as Reader; if (recordEdit.ShowDialog() == true) { Record record = recordEdit.record; record.ReaderId = recordEdit.record.Reader.Id; record.CardId = (int)recordEdit.cardCombo.SelectedItem; record.Gave = (DateTime)recordEdit.dpGave.SelectedDate; record._Return = (DateTime)recordEdit.dpHaveToReturn.SelectedDate; record.Returned = recordEdit.dpReturned.SelectedDate; record.Card = db.Cards.Find(record.CardId); db.Records.Add(record); db.SaveChanges(); readerGridChange(readerGrid, null); } }
public ActionResult Add(Book book, string[] state) { if (db.Books.Where(b => b.Name == book.Name).FirstOrDefault() != null) { ViewBag.Massage = "Книга с таким названием уже существует"; return(View()); } else if (state == null) { ViewBag.Massage = "Вы не выбрали сотояние книги на момент внесения в базу"; return(View()); } db.Books.Add(book); db.SaveChanges(); int id = int.Parse(Session["Id"].ToString()); User user = db.Users.Where(u => u.UserId == id).FirstOrDefault(); UserBook ub = new UserBook(user, book); if (state.Contains("IsHave")) { ub.OnShelf = true; } if (state.Contains("Readed")) { ub.Readed = true; } if (state.Contains("Desired")) { ub.Desired = true; } db.UserBooks.Add(ub); db.SaveChanges(); return(Redirect("/Book/Bookshelf")); }
public ResultModel AddAuthor(AuthorDTO author) { ResultModel result = new ResultModel(); try { Authors item = (Authors)author; _context.Authors.Add(item); _context.SaveChanges(); } catch (Exception ex) { result.Code = OperationStatusEnum.UnexpectedError; result.Message = ex.StackTrace; } return(result); }
private void BeforeAllTests() { context = new TestContext(); books = new Repository <Book>(context); // deletes DB context.Database.EnsureDeleted(); // rekreiramo bazu // creates DB context.Database.EnsureCreated(); books.Insert(new Book { Title = "Book 1" }); books.Insert(new Book { Title = "Book 2" }); context.SaveChanges(); }
public int Redone(int current, DateTime time) { int step = current; try { step--; List <ReadersHistory> history; using (LibContext context = new LibContext()) { history = context.ReadersHistory.Where(c => c.OperationDate <= time).ToList(); history.Reverse(); GenericRepository <Readers> generic = new GenericRepository <Readers>(context); if (step < history.Count && step >= 0) { ReadersHistory pacient = history[step]; string operation = pacient.Operation; context.Database.ExecuteSqlCommand("DISABLE TRIGGER ReadersHistory ON Readers"); context.Database.ExecuteSqlCommand("DISABLE TRIGGER ReadersInsert ON Readers"); if (operation == "inserted") { Readers entity = new Readers { Id = pacient.Id, Surname = pacient.CurrentSurname, Name = pacient.CurrentName, Patronymic = pacient.CurrentPatronymic, BirthDate = pacient.CurrentBirthDate, PassSeria = pacient.CurrentPassSeria, PassNumber = pacient.CurrentPassNumber, Phone = pacient.CurrentPhone, Address = pacient.CurrentAddress, UserId = pacient.CurrentUserId }; using (var scope = context.Database.BeginTransaction()) { context.Readers.Add(entity); context.SaveChanges(); scope.Commit(); } } else if (operation == "updated") { Readers entity = generic.Get(c => c.Id == pacient.Id).FirstOrDefault(); if (entity != null) { entity.Surname = pacient.CurrentSurname; entity.Name = pacient.CurrentName; entity.Patronymic = pacient.CurrentPatronymic; entity.BirthDate = pacient.CurrentBirthDate; entity.PassSeria = pacient.CurrentPassSeria; entity.PassNumber = pacient.CurrentPassNumber; entity.Phone = pacient.CurrentPhone; entity.Address = pacient.CurrentAddress; entity.UserId = pacient.CurrentUserId; generic.Update(entity); } } else if (operation == "deleted") { Readers entity = generic.Get(c => c.Id == pacient.Id).FirstOrDefault(); if (entity != null) { generic.Remove(entity); } } context.Database.ExecuteSqlCommand("ENABLE TRIGGER ReadersHistory ON Readers"); context.Database.ExecuteSqlCommand("ENABLE TRIGGER ReadersInsert ON Readers"); } } } catch (Exception ex) { throw new Exception(ex.Message); } return(step); }
public int Undone(int current, DateTime time) { { int step = current; try { List <IssuesHistory> history; using (LibContext context = new LibContext()) { history = context.IssuesHistory.Where(c => c.OperationDate <= time).ToList(); history.Reverse(); GenericRepository <Issues> generic = new GenericRepository <Issues>(context); if (step < history.Count && step >= 0) { IssuesHistory pacient = history[step]; string operation = pacient.Operation; context.Database.ExecuteSqlCommand("DISABLE TRIGGER IssuesHistory ON Issues"); context.Database.ExecuteSqlCommand("DISABLE TRIGGER IssuesInsert ON Issues"); if (operation == "inserted") { Issues entity = generic.Get(c => c.Id == pacient.Id).FirstOrDefault(); if (entity != null) { generic.Remove(entity); } } else if (operation == "updated") { Issues entity = generic.Get(c => c.Id == pacient.Id).FirstOrDefault(); if (entity != null) { entity.Book = pacient.HistoryBook.Value; entity.Reader = pacient.HistoryReader.Value; entity.IssueDate = pacient.HistoryIssueDate; entity.ReturnDate = pacient.HistoryReturnDate; generic.Update(entity); } } else if (operation == "deleted") { Issues entity = new Issues { Id = pacient.Id, Book = pacient.HistoryBook.Value, Reader = pacient.HistoryReader.Value, IssueDate = pacient.HistoryIssueDate, ReturnDate = pacient.HistoryReturnDate }; using (var scope = context.Database.BeginTransaction()) { context.Issues.Add(entity); context.SaveChanges(); scope.Commit(); } } context.Database.ExecuteSqlCommand("ENABLE TRIGGER IssuesHistory ON Issues"); context.Database.ExecuteSqlCommand("ENABLE TRIGGER IssuesInsert ON Issues"); } } step++; } catch (Exception ex) { throw new Exception(ex.Message); } return(step); } }
public void Save() { context.SaveChanges(); }
public void Create(TEntity item) { _dbSet.Add(item); _context.SaveChanges(); }
public int Undone(int current, DateTime time) { int step = current; try { List <AuthorsHistory> history; using (LibContext context = new LibContext()) { history = context.AuthorsHistory.Where(c => c.OperationDate <= time).ToList(); history.Reverse(); GenericRepository <Authors> generic = new GenericRepository <Authors>(context); if (step < history.Count && step >= 0) { AuthorsHistory pacient = history[step]; string operation = pacient.Operation; context.Database.ExecuteSqlCommand("DISABLE TRIGGER AuthorsHistory ON Authors"); context.Database.ExecuteSqlCommand("DISABLE TRIGGER AuthorsInsert ON Authors"); if (operation == "inserted") { Authors entity = generic.Get(c => c.Id == pacient.Id).FirstOrDefault(); if (entity != null) { generic.Remove(entity); } } else if (operation == "updated") { Authors entity = generic.Get(c => c.Id == pacient.Id).FirstOrDefault(); if (entity != null) { entity.Surname = pacient.HistorySurname; entity.Name = pacient.HistoryName; entity.Patronymic = pacient.HistoryPatronymic; generic.Update(entity); } } else if (operation == "deleted") { Authors entity = new Authors { Id = pacient.Id, Surname = pacient.HistorySurname, Name = pacient.HistoryName, Patronymic = pacient.HistoryPatronymic }; using (var scope = context.Database.BeginTransaction()) { context.Authors.Add(entity); context.SaveChanges(); scope.Commit(); } } context.Database.ExecuteSqlCommand("ENABLE TRIGGER AuthorsHistory ON Authors"); context.Database.ExecuteSqlCommand("ENABLE TRIGGER AuthorsInsert ON Authors"); } } step++; } catch (Exception ex) { throw new Exception(ex.Message); } return(step); }
public void Add(T obj) { db.Set <T>().Add(obj); db.SaveChanges(); }
public int Undone(int current, DateTime time) { { int step = current; try { List <LibrariesHistory> history; using (LibContext context = new LibContext()) { history = context.LibrariesHistory.Where(c => c.OperationDate <= time).ToList(); history.Reverse(); GenericRepository <Libraries> generic = new GenericRepository <Libraries>(context); if (step < history.Count && step >= 0) { LibrariesHistory pacient = history[step]; string operation = pacient.Operation; context.Database.ExecuteSqlCommand("DISABLE TRIGGER LibrariesHistory ON Libraries"); context.Database.ExecuteSqlCommand("DISABLE TRIGGER LibrariesInsert ON Libraries"); if (operation == "inserted") { Libraries entity = generic.Get(c => c.Id == pacient.Id).FirstOrDefault(); if (entity != null) { generic.Remove(entity); } } else if (operation == "updated") { Libraries entity = generic.Get(c => c.Id == pacient.Id).FirstOrDefault(); if (entity != null) { entity.Name = pacient.HistoryName; entity.Phone = pacient.HistoryPhone; entity.Address = pacient.HistoryAddress; entity.City = pacient.HistoryCity.HasValue ? pacient.HistoryCity.Value : 0; generic.Update(entity); } } else if (operation == "deleted") { Libraries entity = new Libraries { Id = pacient.Id, Name = pacient.HistoryName, Phone = pacient.HistoryPhone, Address = pacient.HistoryAddress, City = pacient.HistoryCity.HasValue ? pacient.HistoryCity.Value : 0 }; using (var scope = context.Database.BeginTransaction()) { context.Libraries.Add(entity); context.SaveChanges(); scope.Commit(); } } context.Database.ExecuteSqlCommand("ENABLE TRIGGER CitiesHistory ON Cities"); context.Database.ExecuteSqlCommand("ENABLE TRIGGER LibrariesInsert ON Libraries"); } } step++; } catch (Exception ex) { throw new Exception(ex.Message); } return(step); } }
public void Save() { db.SaveChanges(); }
public int Redone(int current, DateTime time) { int step = current; try { step--; List <SuppliesHistory> history; using (LibContext context = new LibContext()) { history = context.SuppliesHistory.Where(c => c.OperationDate <= time).ToList(); history.Reverse(); GenericRepository <Supplies> generic = new GenericRepository <Supplies>(context); if (step < history.Count && step >= 0) { SuppliesHistory pacient = history[step]; string operation = pacient.Operation; context.Database.ExecuteSqlCommand("DISABLE TRIGGER SuppliesHistory ON Supplies"); context.Database.ExecuteSqlCommand("DISABLE TRIGGER SuppliesInsert ON Supplies"); if (operation == "inserted") { Supplies entity = new Supplies { Id = pacient.Id, Shop = pacient.CurrentShop.Value, Summ = pacient.CurrentSumm, Date = pacient.CurrentDate }; using (var scope = context.Database.BeginTransaction()) { context.Supplies.Add(entity); context.SaveChanges(); scope.Commit(); } } else if (operation == "updated") { Supplies entity = generic.Get(c => c.Id == pacient.Id).FirstOrDefault(); if (entity != null) { entity.Shop = pacient.CurrentShop.Value; entity.Summ = pacient.CurrentSumm; entity.Date = pacient.CurrentDate; generic.Update(entity); } } else if (operation == "deleted") { Supplies entity = generic.Get(c => c.Id == pacient.Id).FirstOrDefault(); if (entity != null) { generic.Remove(entity); } } context.Database.ExecuteSqlCommand("ENABLE TRIGGER SuppliesHistory ON Supplies"); context.Database.ExecuteSqlCommand("ENABLE TRIGGER SuppliesInsert ON Supplies"); } } } catch (Exception ex) { throw new Exception(ex.Message); } return(step); }
public int Redone(int current, DateTime time) { int step = current; try { step--; List <BooksHistory> history; using (LibContext context = new LibContext()) { history = context.BooksHistory.Where(c => c.OperationDate <= time).ToList(); history.Reverse(); GenericRepository <Books> generic = new GenericRepository <Books>(context); if (step < history.Count && step >= 0) { BooksHistory pacient = history[step]; string operation = pacient.Operation; context.Database.ExecuteSqlCommand("DISABLE TRIGGER BooksHistory ON Books"); context.Database.ExecuteSqlCommand("DISABLE TRIGGER BooksInsert ON Books"); if (operation == "inserted") { Books entity = new Books { Id = pacient.Id, Title = pacient.CurrentTitle, Author = pacient.CurrentAuthor.HasValue ? pacient.CurrentAuthor.Value : 0, Department = pacient.CurrentDepartment }; using (var scope = context.Database.BeginTransaction()) { context.Books.Add(entity); context.SaveChanges(); scope.Commit(); } } else if (operation == "updated") { Books entity = generic.Get(c => c.Id == pacient.Id).FirstOrDefault(); if (entity != null) { entity.Title = pacient.CurrentTitle; entity.Author = pacient.CurrentAuthor.HasValue ? pacient.CurrentAuthor.Value : 0; entity.Department = pacient.CurrentDepartment; generic.Update(entity); } } else if (operation == "deleted") { Books entity = generic.Get(c => c.Id == pacient.Id).FirstOrDefault(); if (entity != null) { generic.Remove(entity); } } context.Database.ExecuteSqlCommand("ENABLE TRIGGER BooksHistory ON Books"); context.Database.ExecuteSqlCommand("ENABLE TRIGGER BooksInsert ON Books"); } } } catch (Exception ex) { throw new Exception(ex.Message); } return(step); }