public void Add <T>(T entity) where T : class { if (entity == null) { return; } _managementContext.Set <T>().Add(entity); _managementContext.SaveChanges(); }
public ActionResult Create([Bind(Include = "ID,Street,City,State,Zip,Value,LandlordID,Description")] Property property) { if (ModelState.IsValid) { db.Properties.Add(property); db.SaveChanges(); return(RedirectToAction("Index")); } return(View(property)); }
public ActionResult Create([Bind(Include = "ID,Title,Start,Duration,Location,Coordinator")] Event @event) { if (ModelState.IsValid) { db.Events.Add(@event); db.SaveChanges(); return(RedirectToAction("Index")); } ViewBag.Coordinator = new SelectList(db.Persons, "ID", "First", @event.Coordinator); return(View(@event)); }
public ActionResult Create([Bind(Include = "ID,Event,Person,Timestamp")] RSVP rSVP) { if (ModelState.IsValid) { db.RSVPs.Add(rSVP); db.SaveChanges(); return(RedirectToAction("Index")); } ViewBag.Event = new SelectList(db.Events, "ID", "Title", rSVP.Event); ViewBag.Person = new SelectList(db.Persons, "ID", "First", rSVP.Person); return(View(rSVP)); }
public bool Create(Company company) { try { _mgmtContext.Add(company); _mgmtContext.SaveChanges(); return(true); } catch (Exception ex) { Debug.WriteLine("Message " + ex.Message); return(false); } }
// on creating makes sure sub company object is associated with company public bool Create(SubCompany subCompany) { try { _mgmtContext.Add(subCompany); _mgmtContext.SaveChanges(); return(true); } catch (DbUpdateException due) { Debug.WriteLine(due.Message); return(false); } }
public void SaveManagementChanges() { lock (ManagementContext) { ManagementContext.SaveChanges(); } }
public void DbSet_Add_Set_All_Graph_Added() { //Assert Country country; using (var context = new ManagementContext()) { country = context.Countries.First(); } var customer = new Customer { Name = "Nuevo cliente", Addresses = new Collection <Address>() { new Address() { Region = "Nueva región", Country = country } } }; //Act using (var context = new ManagementContext()) { context.Customers.Add(customer); //Assert Assert.AreEqual(EntityState.Added, context.Entry(country).State, "country State no es Added"); context.SaveChanges(); Assert.AreEqual(3, context.Countries.Count(), "Countries no son 3"); } }
public void DbEntityEntry_State_Modified_Only_Set_Current_Entity() { //Assert var customer = GetFirstDisconnectedCustomerFullyLoaded(); customer.Name = "Cliente modificado"; var address = customer.Addresses.First(); const string newRegion = "Región modificada"; address.Region = newRegion; using (var context = new ManagementContext()) { //Act context.Entry(customer).State = EntityState.Modified; //Assert Assert.AreEqual(EntityState.Unchanged, context.Entry(address).State, "address State no es Unchanged"); //Act context.SaveChanges(); context.Entry(address).Reload(); //Assert Assert.AreNotEqual(address.Region, newRegion, $"Region no es {newRegion}"); } }
public void DbSet_Add_Set_All_Graph_Added() { //Assert Country country; using (var context = new ManagementContext()) { country = context.Countries.First(); } var customer = new Customer { Name = "Nuevo cliente", Addresses = new Collection<Address>() { new Address() { Region = "Nueva región", Country = country } } }; //Act using (var context = new ManagementContext()) { context.Customers.Add(customer); //Assert Assert.AreEqual(EntityState.Added, context.Entry(country).State, "country State no es Added"); context.SaveChanges(); Assert.AreEqual(3, context.Countries.Count(), "Countries no son 3"); } }
public bool UpdateEmployeeProjects(EmployeeProjectViewModel employeeProjectViewModel) { try { Employee emp = _mgmtContext.Employee.Where(e => e._EmpId == employeeProjectViewModel.Id).Include(e => e.EmployeeProjects).ThenInclude(e => e.Project).ToList()[0]; // if employee project is empty then add the selected project in EmployeePoject table if (emp.EmployeeProjects.Count() == 0) { foreach (var pjt in employeeProjectViewModel.Projects) { if (pjt.isProjectSelected) { _mgmtContext.AddRange(new EmployeeProject { employee = emp, Project = _mgmtContext.Project.FirstOrDefault(p => p._PjtId == pjt._PjtId) }); } } } else// when employee projects are in EmployeeProject table { foreach (var pjt in employeeProjectViewModel.Projects) { var empPjt = emp.EmployeeProjects.FirstOrDefault(ep => ep._PjtId == pjt._PjtId); if (pjt.isProjectSelected && empPjt == null) { //project is selected and is not in the table so adding it to the database table emp.EmployeeProjects.Add( new EmployeeProject { employee = emp, _PjtId = pjt._PjtId, Project = _mgmtContext.Project.FirstOrDefault(p => p._PjtId == pjt._PjtId) } ); } else if (!pjt.isProjectSelected && empPjt != null) { // project is not selected but it is in the database table so,removing it emp.EmployeeProjects.Remove(empPjt); } else if (pjt.isProjectSelected && empPjt != null) { //here do nothing as employee project is already in database table } } } _mgmtContext.SaveChanges(); return(true); } catch (Exception ex) { Debug.WriteLine("Message " + ex.Message); return(false); } }
public bool Create(Position position) { try { //checking if same position exists then return false to avoid duplicate creation of same position List <Position> positionsExist = _mgmtContext.Position.Where(p => p.Position_Type == position.Position_Type).ToList(); if (positionsExist.Count() == 1) { return(false); } _mgmtContext.Add(position); _mgmtContext.SaveChanges(); return(true); } catch (DbUpdateException due) { Debug.WriteLine(due.Message); return(false); } }
private void InsertCustomers(int count) { using (var context = new ManagementContext()) { for (int i = 0; i < count; i++) { context.Customers.Add(new Customer() { Name = "Cliente " + i, Code = "C" + i }); } context.SaveChanges(); } }
public IActionResult Create([Bind("SerialNumber, Name, Stock")] Device device) { if (!_managementContext.Devices.Where(d => d.SerialNumber == device.SerialNumber).Any()) { if (ModelState.IsValid) { _managementContext.Add(device); _managementContext.SaveChanges(); return(RedirectToAction(nameof(Index))); } } else { ModelState.AddModelError("SerialNumber", "Serial number already in system"); } return(View(device)); }
public static bool SaveCurrentPaypalBitcoinInformation(Fund fund) { var dc = new ManagementContext(); var funds = dc.Funds.Where(x => x.UserId == fund.UserId).FirstOrDefault(); if (funds == null) { FundsForWriter f = new FundsForWriter(); f.BitCoinId = fund.BitCoinId; f.PaypalAddress = fund.PaypalAddress; f.UserId = fund.UserId; dc.Funds.Add(f); } else { funds.PaypalAddress = fund.PaypalAddress; funds.BitCoinId = fund.BitCoinId; } int c = dc.SaveChanges(); return c > 0; }
public void Save() { db.SaveChanges(); }
public void Update(T entity) { using var context = new ManagementContext(); context.Set <T>().Update(entity); context.SaveChanges(); }
public void Delete(T entity) { using var context = new ManagementContext(); context.Set <T>().Remove(entity); context.SaveChanges(); }
public bool CreateEmployee(EmployeeViewModel employeeViewModel) { try { HashSet <string> addressSet = null; HashSet <string> phoneNumberSet = null; Employee employee = new Employee(); employee.Name = employeeViewModel.EmployeeName; employee.Joined_Date = employeeViewModel.Joined_Date; // employee.Leave_Date = employeeViewModel.Joined_Date; employee.Salary = employeeViewModel.Salary; employee.SubCompany = _mgmtContext.SubCompany.Where(c => c.Id == employeeViewModel.SubCompanyId).ToList()[0]; employee.Positions = _mgmtContext.Position.Where(c => c.Id == employeeViewModel.PositionId).ToList()[0]; //checking optional phone numbers list is not null && empty //adding unique phone number only if (employeeViewModel.PhoneNumbers != null && employeeViewModel.PhoneNumbers.Count() > 0) { phoneNumberSet = new HashSet <string>(); foreach (string addr in employeeViewModel.PhoneNumbers) { if (!String.IsNullOrEmpty(addr)) { phoneNumberSet.Add(addr); } } } //checking optional address list is not null && empty //adding unique address only if (employeeViewModel.Addresses != null && employeeViewModel.Addresses.Count() > 0) { addressSet = new HashSet <string>(); foreach (string phn in employeeViewModel.Addresses) { if (!String.IsNullOrEmpty(phn)) { addressSet.Add(phn); } } } List <Phone> phones = new List <Phone>(); List <Address> addresses = new List <Address>(); Phone phone = new Phone(); phone.Phone_Number = employeeViewModel._PhoneNumber; phone.Employee = employee; phones.Add(phone); Address address = new Address(); address.Location = employeeViewModel._Address; address.Employee = employee; addresses.Add(address); if (phoneNumberSet != null) { foreach (string phn in phoneNumberSet) { phones.Add(new Phone { Phone_Number = phn, Employee = employee }); } } if (addressSet != null) { foreach (string addr in addressSet) { addresses.Add(new Address { Location = addr, Employee = employee }); } } employee.Addresses = addresses; employee.Phones = phones; _mgmtContext.Add(employee); _mgmtContext.SaveChanges(); return(true); } catch (Exception ex) { Console.WriteLine("Message " + ex.Message); return(false); } }
public void Save() { _context.SaveChanges(); }