public static bool Exists(int entityId, int referenceFrameworkId) { ThisEntity entity = new ThisEntity(); try { using (var context = new EntityContext()) { DBEntity item = context.Entity_ReferenceFramework .FirstOrDefault(s => s.EntityId == entityId && s.ReferenceFrameworkId == referenceFrameworkId); if (item != null && item.Id > 0) { return(true); } } } catch (Exception ex) { LoggingHelper.LogError(ex, thisClassName + ".Exists"); } return(false); } //
//public bool OnetSaveList( int parentEntityId, int categoryId, List<string> list, ref SaveStatus status ) //{ // if ( list == null || list.Count == 0 ) // return true; // bool isAllValid = true; // CredentialAlignmentObjectProfile item = new CredentialAlignmentObjectProfile(); // foreach ( string code in list ) // { // item = new CredentialAlignmentObjectProfile(); // //look up SOC // var records = CodesManager.SOC_Get( code ); // foreach ( var record in records ) // { // if ( record != null && record.Id > 0 ) // { // item.Id = record.Id; // item.TargetNodeName = record.Title; // item.Description = record.Description; // item.CodedNotation = record.Code; // item.TargetNode = record.URL; // Add( parentEntityId, categoryId, item, ref status, false ); // } // } // } // return isAllValid; //} //public bool CIPSaveList( int parentEntityId, int categoryId, List<string> list, ref SaveStatus status ) //{ // if ( list == null || list.Count == 0 ) // return true; // bool isAllValid = true; // CredentialAlignmentObjectProfile item = new CredentialAlignmentObjectProfile(); // foreach ( string code in list ) // { // item = new CredentialAlignmentObjectProfile(); // //look up SOC // var records = CodesManager.SOC_Get( code ); // foreach ( var record in records ) // { // if ( record != null && record.Id > 0 ) // { // item.Id = record.Id; // item.TargetNodeName = record.Title; // item.Description = record.Description; // item.CodedNotation = record.Code; // item.TargetNode = record.URL; // Add( parentEntityId, categoryId, item, ref status, false ); // } // } // } // return isAllValid; //} /// <summary> /// Add a Entity framework Item /// </summary> /// <param name="parentEntityId"></param> /// <param name="categoryId"></param> /// <param name="entity"></param> /// <param name="status"></param> /// <returns></returns> public int Add(int parentEntityId, int categoryId, CredentialAlignmentObjectProfile entity, ref SaveStatus status, bool warningOnDuplicates) { DBEntity efEntity = new DBEntity(); using (var context = new EntityContext()) { try { //first ensure not a duplicate (until interface/search prevents dups) //EnumeratedItem entity = Get( parentEntityId, categoryId, codeID ); //if ( entity != null && entity.Id > 0 ) //{ // status.AddWarning( "Warning: the selected code already exists!" ); // return 0; //} ReferenceFramework rf = new ReferenceFramework() { CategoryId = categoryId, CodedNotation = entity.CodedNotation, Name = entity.TargetNodeName, Description = entity.Description, TargetNode = entity.TargetNode }; //add or update, returns rfm.Id if OK if (!rfm.Save(rf, ref status)) { return(0); } //check if a duplicate if (Exists(parentEntityId, rf.Id)) { if (warningOnDuplicates) { status.AddWarning(string.Format("Warning - Duplicate encountered for categoryId: {0}, entityId: {1}, Name: {2}, ReferenceId: {3}, CodedNotation: {4}", categoryId, parentEntityId, entity.TargetNodeName, rf.Id, rf.CodedNotation)); } return(0); } //save efEntity.EntityId = parentEntityId; efEntity.CategoryId = categoryId; efEntity.ReferenceFrameworkId = rf.Id; efEntity.Created = System.DateTime.Now; context.Entity_ReferenceFramework.Add(efEntity); // submit the change to database int count = context.SaveChanges(); if (count > 0) { return(efEntity.Id); } else { string message = string.Format(thisClassName + ".Add Failed", "Attempted to add a Entity_ReferenceFramework item. The process appeared to not work, but was not an exception, so we have no message, or no clue. parentId: {0}, CategoryId: {1}, Name: {2}, ReferenceId: {3}, CodedNotation: {4}", parentEntityId, categoryId, entity.TargetNodeName, rf.Id, rf.CodedNotation); //?no info on error status.AddWarning(thisClassName + "Error - the add was not successful. \r\n" + message); //EmailManager.NotifyAdmin( thisClassName + ".ItemAdd Failed", message ); } } catch (Exception ex) { string message = FormatExceptions(ex); LoggingHelper.LogError(ex, thisClassName + string.Format(".Add(), parentId: {0}, CategoryId: {1}, Name: {2}, CodedNotation: {3}", parentEntityId, categoryId, entity.TargetNodeName, entity.CodedNotation)); status.AddError(thisClassName + ".Add() - Error - the save was not successful. \r\n" + message); } } return(efEntity.Id); }