Exemple #1
0
        /// <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);
        }
 protected override void Dispose(bool disposing)
 {
     if (disposing)
     {
         db.Dispose();
     }
     base.Dispose(disposing);
 }