public void InitializeProduct() { if (_db.Products.Any()) { return; } using (_db.Database.BeginTransaction()) { _db.Sections.AddRange(TestData.Sections); _db.Database.ExecuteSqlRaw("SET IDENTITY_INSERT [dbo].[Sections] ON"); _db.SaveChanges(); _db.Database.ExecuteSqlRaw("SET IDENTITY_INSERT [dbo].[Sections] OFF"); _db.Database.CommitTransaction(); } using (_db.Database.BeginTransaction()) { _db.Brands.AddRange(TestData.Brands); _db.Database.ExecuteSqlRaw("SET IDENTITY_INSERT [dbo].[Brands] ON"); _db.SaveChanges(); _db.Database.ExecuteSqlRaw("SET IDENTITY_INSERT [dbo].[Brands] OFF"); _db.Database.CommitTransaction(); } using (_db.Database.BeginTransaction()) { _db.Products.AddRange(TestData.Products); _db.Database.ExecuteSqlRaw("SET IDENTITY_INSERT [dbo].[Products] ON"); _db.SaveChanges(); _db.Database.ExecuteSqlRaw("SET IDENTITY_INSERT [dbo].[Products] OFF"); _db.Database.CommitTransaction(); } }
private void InitializeProducts() { var db = _db.Database; if (_db.Products.Any() /*&& _db.Employees.Any()*/) { return; } using (db.BeginTransaction()) { _db.Sections.AddRange(TestData.Sections); db.ExecuteSqlRaw("SET IDENTITY_INSERT [dbo].[ProductSection] ON"); _db.SaveChanges(); db.ExecuteSqlRaw("SET IDENTITY_INSERT [dbo].[ProductSection] OFF"); db.CommitTransaction(); } using (var transaction = db.BeginTransaction()) { _db.Brands.AddRange(TestData.Brands); db.ExecuteSqlRaw("SET IDENTITY_INSERT [dbo].[ProductBrand] ON"); _db.SaveChanges(); db.ExecuteSqlRaw("SET IDENTITY_INSERT [dbo].[ProductBrand] OFF"); transaction.Commit(); } using (var transaction = db.BeginTransaction()) { _db.Products.AddRange(TestData.Products); db.ExecuteSqlRaw("SET IDENTITY_INSERT [dbo].[Products] ON"); _db.SaveChanges(); db.ExecuteSqlRaw("SET IDENTITY_INSERT [dbo].[Products] OFF"); transaction.Commit(); } using (var transaction = db.BeginTransaction()) { _db.Employees.AddRange(TestData._Employees); db.ExecuteSqlRaw("SET IDENTITY_INSERT [dbo].[Employees] ON"); _db.SaveChanges(); db.ExecuteSqlRaw("SET IDENTITY_INSERT [dbo].[Employees] OFF"); transaction.Commit(); } }
public IActionResult DeleteConfirm(int?id) { var product = _db.Products.FirstOrDefault(p => p.Id == id); if (id != null) { _db.Products.Remove(product); _db.SaveChanges(); return(RedirectToAction("Index")); } return(NotFound()); }
public void Edit(Employee Employee) { if (Employee is null) { throw new ArgumentNullException(nameof(Employee)); } Employee db_item = GetById(Employee.Id); if (db_item is null) { return; } _db.Employees.Attach(db_item); _Logger.LogInformation("Редактирование сотрудника: [{0}]{1} {2} {3}", Employee.Id, Employee.Surname, Employee.FirstName, Employee.Patronymic); db_item.FirstName = Employee.FirstName; db_item.Surname = Employee.Surname; db_item.Patronymic = Employee.Patronymic; db_item.Age = Employee.Age; _db.SaveChanges(); }