public static ThisEntity Get(int parentId, int recordId) { ThisEntity entity = new ThisEntity(); if (parentId < 1 || recordId < 1) { return(entity); } try { using (var context = new EntityContext()) { DBEntity from = context.Entity_IdentifierValue .SingleOrDefault(s => s.Id == recordId && s.EntityId == parentId); if (from != null && from.Id > 0) { MapFromDB(from, entity); } } } catch (Exception ex) { LoggingHelper.LogError(ex, thisClassName + ".Get"); } return(entity); } //
/// <summary> /// Add an Entity_IdentifierValueManager /// </summary> /// <param name="parentUid"></param> /// <param name="entity"></param> /// <param name="messages"></param> /// <returns></returns> private int Add(Entity parent, ThisEntity entity, ref SaveStatus status) { int id = 0; int count = 0; //Entity parent = EntityManager.GetEntity( parentUid ); if (parent == null || parent.Id == 0) { status.AddError("Error - the parent entity was not found."); return(0); } using (var context = new EntityContext()) { DBEntity efEntity = new DBEntity(); try { efEntity = new DBEntity(); MapToDB(entity, efEntity); efEntity.EntityId = parent.Id; efEntity.Created = System.DateTime.Now; context.Entity_IdentifierValue.Add(efEntity); // submit the change to database count = context.SaveChanges(); if (count > 0) { id = efEntity.Id; return(efEntity.Id); } else { //?no info on error status.AddError("Error - the add was not successful."); string message = thisClassName + string.Format(".Add Failed", "Attempted to add a Entity_IdentifierValue for a profile. The process appeared to not work, but there was no exception, so we have no message, or no clue. Parent Profile: {0}, Type: {1}, learningOppId: {2}, createdById: {3}", parent.EntityUid, parent.EntityType, entity.IdentifierType); EmailManager.NotifyAdmin(thisClassName + ".Add Failed", message); } } catch (System.Data.Entity.Validation.DbEntityValidationException dbex) { string message = HandleDBValidationError(dbex, thisClassName + ".Add() ", "Entity_Entity_IdentifierValue"); status.AddError("Error - the save was not successful. " + message); LoggingHelper.LogError(dbex, thisClassName + string.Format(".Save(), Parent: {0} ({1})", parent.EntityBaseName, parent.EntityBaseId)); } catch (Exception ex) { string message = FormatExceptions(ex); status.AddError("Error - the save was not successful. " + message); LoggingHelper.LogError(ex, thisClassName + string.Format(".Save(), Parent: {0} ({1})", parent.EntityBaseName, parent.EntityBaseId)); } } return(id); }
} // public static void MapFromDB(DBEntity from, ThisEntity to) { to.Id = from.Id; to.EntityId = from.EntityId; to.IdentityValueTypeId = from.IdentityValueTypeId; to.Name = from.Name; to.Description = from.Description; to.IdentifierType = from.IdentifierType; to.IdentifierValueCode = from.IdentifierValueCode; to.Created = ( DateTime )from.Created; }
public static void MapToDB(ThisEntity from, DBEntity to) { if (from.Id == 0) { to.Id = from.Id; to.EntityId = from.EntityId; } to.IdentityValueTypeId = from.IdentityValueTypeId; to.Name = from.Name; to.Description = from.Description; to.IdentifierType = from.IdentifierType; to.IdentifierValueCode = from.IdentifierValueCode; }