Как повторить предыдущие действия в Exception в EF 6 - PullRequest
0 голосов
/ 03 мая 2018

У меня проблема с повторением предыдущих операций, когда в методе SaveChanges ошибки Entity Framework.

есть ошибка

Ниже приведен кодовый блок

 public static int SaveChangesTask(this DbContext db)
    {
        int result = -1;int countLoop = 0;
        bool continueLoop = true;
        var modifiedOrAddedEntities = db.ChangeTracker.Entries().Where(a => a.State != EntityState.Detached
           && a.State != EntityState.Unchanged).ToList();
        while (continueLoop && countLoop<3)
        {

            try
            {
                result= db.SaveChanges();
                continueLoop = false;
            }
            catch(Exception ex)
            {
                string error = ex.ToSystemException();
                if(error.ToLowerInvariant().Contains("ORA-00060".ToLowerInvariant()) || error.ToLowerInvariant().Contains("deadlock"))
                {

                    foreach (var item in modifiedOrAddedEntities)
                    {
                        db.Entry(item).State = item.State;
                    }
                    countLoop++;
                    Random rnd = new Random();

                    System.Threading.Thread.Sleep(rnd.Next(1, 5)* 1000);
                }
                else
                {
                    throw ex;
                }
            }
        }
        return result;
    }

Но когда я хочу добавить старые объекты отслеживания в контекст, Entity Framework создает исключение следующим образом "The entity type DbEntityEntry is not part of the model for the current context"

...