public Boolean CreateBuilding(ref BuildingBDO buildBDO, ref string message) { message = "Building Added Successfully"; bool ret = true; try { Building b = new Building(); ConvertBuildingBDOToBuilding(buildBDO,b); using (var DCEnt = new DCFIEntities()) { DCEnt.Buildings.Add(b); DCEnt.Entry(b).State = System.Data.Entity.EntityState.Added; int num = DCEnt.SaveChanges(); buildBDO.BuildingCode = b.BuildingCode; if (num < 1) { ret = false; message = "Adding of Building failed"; } } } catch (DbEntityValidationException dbEx) { foreach (var validationErrors in dbEx.EntityValidationErrors) { foreach (var validationError in validationErrors.ValidationErrors) { Trace.TraceInformation("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage); } } } return ret; }
public Boolean CreateBuilding(ref BuildingBDO buildBDO, ref string message) { message = "Building Added Successfully"; bool ret = true; Building b = new Building(); ConvertBuildingBDOToBuilding(buildBDO,b); using (var DCEnt = new DCFIEntities()) { DCEnt.Buildings.Add(b); DCEnt.Entry(b).State = System.Data.Entity.EntityState.Added; int num = DCEnt.SaveChanges(); buildBDO.BuildingCode = b.BuildingCode; if (num != 1) { ret = false; message = "Adding of Building failed"; } } return ret; }
private void ConvertBuildingBDOToBuilding(BuildingBDO bdo, Building b) { b.BuildingCode = bdo.BuildingCode; b.BuildingName = bdo.BuildingName; b.Deactivated = bdo.Deactivated; b.Description = bdo.Description; b.Rooms =ToRoomList(bdo.Rooms); }
private void ConvertBuildingToBuildingBDO(Building b, BuildingBDO bdo) { bdo.BuildingCode = b.BuildingCode; bdo.BuildingName = b.BuildingName; bdo.Deactivated = b.Deactivated; bdo.Description = b.Description; // bdo.Rooms = ToRoomBDOList(b.Rooms); }
private Boolean CompareBuilding(Building inDB, Building inNew){ if (inDB.BuildingCode == inNew.BuildingCode && inDB.BuildingName == inNew.BuildingName && inDB.Description == inNew.Description) return true; else return false; }
public Boolean UpdateBuilding(ref BuildingBDO buildBDO, ref string message) { message = "Building updated successfully."; Boolean ret = true; List<Room> toRemove; List<Room> toAdd; List<Room> toUpdate; Building b = new Building(); try { ConvertBuildingBDOToBuilding(buildBDO, b); Building buildingInDB = new Building(); using (var DCEnt = new DCFIEntities()) { var buildingCode = buildBDO.BuildingCode; buildingInDB = (from bee in DCEnt.Buildings where bee.BuildingCode == buildingCode select bee).FirstOrDefault(); if (buildingInDB == null) { throw new Exception("No Building with BuildingCode " + buildBDO.BuildingCode); } // 1st Part if (buildingInDB.Rooms.Count == 0) { foreach (Room rm in b.Rooms) { buildingInDB.Rooms.Add(rm); } } else { toRemove = new List<Room>(); toAdd = new List<Room>(); toUpdate = new List<Room>(); if (buildingInDB.Rooms.Count < b.Rooms.Count) { foreach (Room r in b.Rooms) { List<Room> rIn = buildingInDB.Rooms.Where(ro => ro.RoomCode == r.RoomCode).ToList<Room>(); if (rIn.Count == 0) toAdd.Add(r); } foreach (Room r in buildingInDB.Rooms) { int co = b.Rooms.Where(ro => ro.RoomCode == r.RoomCode).Count(); if (co == 0) toRemove.Add(r); } } else if (buildingInDB.Rooms.Count > b.Rooms.Count) { foreach (Room r in b.Rooms) { List<Room> rIn = buildingInDB.Rooms.Where(ro => ro.RoomCode == r.RoomCode).ToList<Room>(); if (rIn.Count == 0) toAdd.Add(r); } foreach (Room r in buildingInDB.Rooms) { int co = b.Rooms.Where(ro => ro.RoomCode == r.RoomCode).Count(); if (co == 0) toRemove.Add(r); } } else if (buildingInDB.Rooms.Count == b.Rooms.Count) { foreach (Room r in b.Rooms) { List<Room> rIn = buildingInDB.Rooms.Where(ro => ro.RoomCode == r.RoomCode).ToList<Room>(); if (rIn.Count == 0) toAdd.Add(r); } foreach (Room r in buildingInDB.Rooms) { int co = b.Rooms.Where(ro => ro.RoomCode == r.RoomCode).Count(); if (co == 0) toRemove.Add(r); } } foreach (Room r in b.Rooms) { List<Room> rIn = buildingInDB.Rooms.Where(ro => ro.RoomCode == r.RoomCode).ToList<Room>(); if (rIn.Exists(p => p.RoomCode == r.RoomCode)) { if (!CompareRoom(rIn[0], r)) { toUpdate.Add(r); } } } foreach (Room r in toAdd) { buildingInDB.Rooms.Add(r); } foreach (Room r in toRemove) { DCEnt.Rooms.Remove(r); DCEnt.Entry(r).State = System.Data.Entity.EntityState.Deleted; } foreach (Room r in toUpdate) { var roomIn = (from rm in DCEnt.Rooms where rm.RoomId == r.RoomId select rm).FirstOrDefault<Room>(); DCEnt.Rooms.Remove(roomIn); roomIn.BuildingCode = r.BuildingCode; roomIn.Capacity = r.Capacity; roomIn.Deactivated = r.Deactivated; roomIn.Description = r.Description; roomIn.RoomCode = r.RoomCode; roomIn.RoomNumber = r.RoomNumber; roomIn.RoomId = r.RoomId; DCEnt.Rooms.Attach(roomIn); DCEnt.Entry(roomIn).State = System.Data.Entity.EntityState.Modified; } } if (CompareBuilding(buildingInDB, b)) { DCEnt.SaveChanges(); // DCEnt.Entry(r).State = System.Data.Entity.EntityState.Modified; } else { buildingInDB.BuildingCode = b.BuildingCode; buildingInDB.BuildingName = b.BuildingName; buildingInDB.Description = b.Description; DCEnt.Entry(buildingInDB).State = System.Data.Entity.EntityState.Modified; DCEnt.SaveChanges(); } } } catch (DbEntityValidationException dbEx) { foreach (var validationErrors in dbEx.EntityValidationErrors) { foreach (var validationError in validationErrors.ValidationErrors) { Trace.TraceInformation("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage); } } } return ret; }
public Boolean UpdateBuilding(ref BuildingBDO buildBDO, ref string message) { message = "Building updated successfully."; Boolean ret = true; Building b = new Building(); ConvertBuildingBDOToBuilding(buildBDO, b); Building buildingInDB = new Building(); using (var DCEnt = new DCFIEntities()) { var buildingCode = buildBDO.BuildingCode; buildingInDB = (from bee in DCEnt.Buildings where bee.BuildingCode == buildingCode select bee).FirstOrDefault(); if (buildingInDB == null) { throw new Exception("No Building with BuildingCode " + buildBDO.BuildingCode); } // 1st Part if (buildingInDB.Rooms.Count == 0) { foreach (Room rm in b.Rooms) { buildingInDB.Rooms.Add(rm); } } else if (buildingInDB.Rooms.Count < b.Rooms.Count) { //compare 2 lists check the non existing to the other IEnumerable<Room> rmToAdd = b.Rooms.Except(buildingInDB.Rooms); if (rmToAdd != null) { foreach (Room child in rmToAdd) { buildingInDB.Rooms.Add(child); } } IEnumerable<Room> rmToRemove = buildingInDB.Rooms.Except(b.Rooms); if (rmToRemove != null) { foreach (Room child in rmToRemove) { buildingInDB.Rooms.Add(child); } } } else if (buildingInDB.Rooms.Count > b.Rooms.Count) { //compare 2 lists check the non existing to the other IEnumerable<Room> rmToAdd = b.Rooms.Except(buildingInDB.Rooms); if (rmToAdd != null) { foreach (Room child in rmToAdd) { buildingInDB.Rooms.Add(child); } } // TBC IEnumerable<Room> rmToRemove = buildingInDB.Rooms.Except(b.Rooms); if (rmToRemove != null) { foreach (Room child in rmToRemove) { buildingInDB.Rooms.Add(child); } } } else if (buildingInDB.Rooms.Count == b.Rooms.Count) { //compare 2 lists check the non existing to the other IEnumerable<Room> rmToAdd = b.Rooms.Except(buildingInDB.Rooms); if (rmToAdd != null) { foreach (Room child in rmToAdd) { buildingInDB.Rooms.Add(child); } } IEnumerable<Room> rmToRemove = buildingInDB.Rooms.Except(b.Rooms); if (rmToRemove != null) { foreach (Room child in rmToRemove) { buildingInDB.Rooms.Add(child); } } } } using (var DC = new DCFIEntities()) { buildingInDB = b; DC.Entry(buildingInDB).State = System.Data.Entity.EntityState.Modified; foreach (Room r in buildingInDB.Rooms) DC.Entry(r).State = r.RoomId == 0 ? System.Data.Entity.EntityState.Added : System.Data.Entity.EntityState.Modified; int num = DC.SaveChanges(); if (num > 0) { // ret = false; message = "No building is updated."; } } return ret; }