Пример #1
0
        public virtual void SaveChanges()
        {
            try
            {
                int maxTries          = 10;
                int counter           = 0;
                WorkshopDBContext ctx = getContext();
                ctx.ChangeTracker.DetectChanges();

                //use database wins
                bool saveFailed;
                do
                {
                    saveFailed = false;

                    try
                    {
                        ctx.SaveChanges();
                    }
                    catch (DbUpdateException ex)
                    {
                        counter++;
                        if (counter >= maxTries)
                        {
                            throw new WorkShopDbException(ex.Message);
                        }

                        saveFailed = true;

                        // Update the values of the entity that failed to save from the store
                        //ex.Entries.Single().Reload();


                        // Update original values from the database

                        //try
                        //{
                        var entry = ex.Entries.Single();
                        entry.OriginalValues.SetValues(entry.GetDatabaseValues());
                        //}
                        //catch(ArgumentNullException exx)
                        //{
                        //    ex.Entries.Single().Reload();
                        //}
                    }
                } while (saveFailed);
            }
            catch (Exception e)
            {
                Logger.Log("error", logLevel.ERROR, e.Message);
                throw new WorkShopDbException(e.Message);
            }
        }
Пример #2
0
        /*
         * public virtual void Remove<T>(List<T> entity) where T : IEntity
         * {
         *  WorkshopDBContext ctx = getContext();
         *  ctx.Set<T>().RemoveRange(entity);
         * }
         */

        //public void Clear<T>() where T : IEntity
        //{
        //    ctx.Set<T>().RemoveRange(GetList<T>());
        //    ctx.ChangeTracker.DetectChanges();
        //    ctx.SaveChanges();
        //}

        public virtual void Delete()
        {
            try
            {
                WorkshopDBContext ctx = getContext();
                try
                {
                    ctx.SaveChanges();
                }
                catch (Exception e)
                {
                    Logger.Log("error", logLevel.ERROR, e.Message);
                    //do nothing
                }
                //ctx.Dispose();
                //ctx = new WorkshopTestDBContext();
                MurderAllConnections(ctx);

                ctx.Database.Delete();
                try
                {
                    ctx.SaveChanges();
                }
                catch (Exception e)
                {
                    Logger.Log("error", logLevel.ERROR, e.Message);
                    //do nothing
                }

                DataAccessDriver.resetContext();
            }
            catch (Exception e)
            {
                Logger.Log("error", logLevel.ERROR, e.Message);
                throw new WorkShopDbException(e.Message);
            }
        }
Пример #3
0
 public static void resetContext()
 {
     ctx.SaveChanges();
     ctx.Dispose();
     ctx = null;
 }