/// <summary> /// Removes test data from database, called from all tests that create data /// </summary> /// <remarks> /// If you have issues with data not disposing then set break-points /// in the emppty try/catch statements to figure out the issue. More likely /// than not the interface, in this case IBaseEntity was not implemented on /// one of the classes. /// /// The try-catches allow us to continue and throw an exception message in /// the tear down event TeardownTestBase for any test. /// /// Empty try/catches are okay here as you should be using this only for /// unit testing and hopefully on a non production database. /// /// </remarks> public bool AnnihilateData(List <object> mAnnihilateList) { bool mAnnihilateDataSuccessful = false; using (var destroyContext = new PersonEntities()) { for (int i = mAnnihilateList.Count - 1; i >= 0; i--) { try { var currentObject = mAnnihilateList[i]; var existingItem = destroyContext .Set(currentObject.GetType()) .Find(((IBaseEntity)currentObject).Identifier); if (existingItem != null) { try { var attachedEntry = destroyContext.Entry(existingItem); attachedEntry.CurrentValues.SetValues(currentObject); destroyContext.Set(existingItem.GetType()).Attach(existingItem); destroyContext.Set(existingItem.GetType()).Remove(existingItem); } catch (Exception) { // ignore nothing do to as the object was not added in properly. } } else { var item = currentObject.GetType(); } } catch (Exception) { //catch and continue save what we can } } try { var resultCount = destroyContext.SaveChanges(); var annihlationCount = mAnnihilateList.Count; mAnnihilateDataSuccessful = (resultCount == annihlationCount); } catch (Exception) { // keep on going } finally { destroyContext.Dispose(); } } return(mAnnihilateDataSuccessful); }
/// <summary> /// generic select from document T class is class is document_class /// </summary> /// <typeparam name="T"></typeparam> /// <returns></returns> public List <Document> GetDocuments <T>(int Pid) where T : BaseClassCreatedModify { var className = typeof(T).Name; var classes = entities.Set <T>(); var documentClasses = entities.document_class.Include(x => x.Document).Where(c => c.class_id == Pid && c.classValue == className).ToList(); documentClasses.ForEach(x => { x.Document.DocumentOwner = classes.FirstOrDefault(x => x.Id == Pid); }); return(documentClasses.Select(x => x.Document).ToList()); }
public List <RelCommunicationClass> GetCommunications <T>(int Pid) where T : BaseClassCreatedModify { var person = entities.person.FirstOrDefault(c => c.Id == Pid); if (person != null) { var classname = typeof(T).Name; var classes = entities.Set <T>(); var oldOnes = entities.communication_class.Include(x => x.Communication).Where(x => x.Communication == null).ToList(); var communication = entities.communication_class.Include(x => x.Communication).Where(x => x.Communication.PersonId == Pid).ToList(); communication.ForEach(x => { x.Communication.Document = entities.documents.FirstOrDefault(c => c.Id == x.Communication.DocumentId); x.sender = classes.FirstOrDefault(d => d.Id == x.ClassId); }); Console.WriteLine(); return(communication); } return(null); }
public virtual void Create(T toAdd) { entities.Set <T>().Add(toAdd); entities.SaveChanges(); }