Beispiel #1
0
        public override int SaveChanges()
        {
            DateTime now = DateTime.Now;

            foreach (var item in ChangeTracker.Entries().Where(x => x.State == EntityState.Modified))
            {
                ((Entity)item.Entity).Modified = now;
            }

            foreach (var item in ChangeTracker.Entries().Where(x => x.State == EntityState.Added))
            {
                ((Entity)item.Entity).Modified = now;
                ((Entity)item.Entity).Created  = now;
            }

            foreach (var item in ChangeTracker.Entries().Where(x => x.State == EntityState.Deleted))
            {
                ((Entity)item.Entity).IsDeleted = true;
                item.State = EntityState.Modified;
            }

            try
            {
                return(base.SaveChanges());
            }
            catch (DbUpdateConcurrencyException ex)
            {
                var myEx = new MyConcurrencyException();

                myEx.UserWins = () =>
                {
                    foreach (var item in ex.Entries)
                    {
                        item.OriginalValues.SetValues(item.GetDatabaseValues());
                    }
                    SaveChanges();
                };

                myEx.DbWins = () =>
                {
                    foreach (var item in ex.Entries)
                    {
                        item.CurrentValues.SetValues(item.GetDatabaseValues());
                    }
                };

                throw myEx;
            }
        }
        public override int SaveChanges() // Jedes mal wenn der Kontext gespeichert wird !!!
        {
            DateTime now = DateTime.Now;  // zwischenspeichern !!!1111elf

            // Im Changetracker schauen, was sich alles verändert hat:
            foreach (var item in ChangeTracker.Entries().Where(x => x.State == EntityState.Modified))
            {
                ((Entity)item.Entity).ModifiedDate = now;
            }
            foreach (var item in ChangeTracker.Entries().Where(x => x.State == EntityState.Added))
            {
                ((Entity)item.Entity).ModifiedDate = now;
                ((Entity)item.Entity).CreationDate = now;
            }
            // Variante: SoftDelete
            foreach (var item in ChangeTracker.Entries().Where(x => x.State == EntityState.Deleted))
            {
                item.State = EntityState.Modified;
                ((Entity)item.Entity).ModifiedDate = now;
                ((Entity)item.Entity).DeletedDate  = now;
                ((Entity)item.Entity).IsDeleted    = true;

                // Daten anonymisiert (bez DSGVO)
                if (((Entity)item.Entity) is AircraftType a)
                {
                    a.Manufacturer = Guid.NewGuid().ToString();
                }
            }

            try
            {
                return(base.SaveChanges());
            }
            catch (DbUpdateConcurrencyException ex)
            {
                string message = $"In der Zwischenzeit wurde die Datenbank von jemand anderem verändert.{Environment.NewLine} Sollen die Änderungen überschrieben werden [UserWins]? Ansonsten [DBWins] ";

                foreach (var item in ex.Entries)
                {
                    var proposedValues = item.CurrentValues;
                    var databaseValues = item.GetDatabaseValues();
                    message += $"{Environment.NewLine}{item.Entity.ToString()}"; // <------------
                    foreach (var property in proposedValues.Properties)
                    {
                        var proposedValue = proposedValues[property];
                        var databaseValue = databaseValues[property];
                        message += $"{Environment.NewLine}Proposed:{proposedValue} \tDatabase:{databaseValue}";
                    }
                }

                MyConcurrencyException myEx = new MyConcurrencyException(message);
                myEx.UserWins = () =>
                {
                    foreach (var item in ex.Entries)
                    {
                        // Refresh original values to bypass next concurrency check
                        item.OriginalValues.SetValues(item.GetDatabaseValues());
                    }
                    SaveChanges();
                };
                myEx.DBWins = () =>
                {
                    foreach (var item in ex.Entries)
                    {
                        item.CurrentValues.SetValues(item.GetDatabaseValues());
                        item.OriginalValues.SetValues(item.GetDatabaseValues());
                        item.State = EntityState.Unchanged;
                    }
                    SaveChanges();
                };

                throw myEx;
            }
            catch (DbUpdateException ex)
            {
                // Ungültige Daten für die DB eingegeben
                return(base.SaveChanges());
            }
        }