Esempio n. 1
0
        public static void DbUpdateConcurrencyExceptionHandler(Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry entry, ILogger logger)
        {
            var    proposedValues = entry.CurrentValues;
            var    databaseValues = entry.GetDatabaseValues();
            string msg            = "";

            foreach (var property in proposedValues.Properties)
            {
                Object proposedValue = null;
                Object databaseValue = null;
                if (proposedValues?.Properties?.Contains(property) == true)
                {
                    proposedValue = proposedValues[property];
                }
                if (databaseValues?.Properties?.Contains(property) == true)
                {
                    databaseValue = databaseValues[property];
                }

                msg += Environment.NewLine + "property: " + property + Environment.NewLine;
                msg += "\tproposedValue: " + proposedValue + Environment.NewLine;
                msg += "\tdatabaseValue: " + databaseValue + Environment.NewLine;
                logger.LogError(msg);
                // TODO: decide which value should be written to database
                // proposedValues[property] = <value to be saved>;
            }
            throw new NotSupportedException(
                      "Don't know how to handle concurrency conflicts for "
                      + entry.Metadata.Name + ": " + msg);
            // Refresh original values to bypass next concurrency check
            entry.OriginalValues.SetValues(databaseValues);
        }
Esempio n. 2
0
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member 'Utils.DbUpdateConcurrencyExceptionHandler(EntityEntry, ILogger)'
        public static ResultBase DbUpdateExceptionHandler(Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry entry, ILogger logger)
#pragma warning restore CS1591 // Missing XML comment for publicly visible type or member 'Utils.DbUpdateConcurrencyExceptionHandler(EntityEntry, ILogger)'
        {
            var    proposedValues = entry.CurrentValues;
            var    databaseValues = entry.GetDatabaseValues();
            string msg            = string.Empty;

#pragma warning disable CS0168 // The variable 'res' is declared but never used
            ResultBase res;
#pragma warning restore CS0168 // The variable 'res' is declared but never used
            try
            {
                foreach (var property in proposedValues.Properties)
                {
                    object proposedValue = null;
                    object databaseValue = null;
                    if (proposedValues.Properties?.Contains(property) == true)
                    {
                        proposedValue = proposedValues[property];
                    }
                    if (databaseValues?.Properties?.Contains(property) == true)
                    {
                        databaseValue = databaseValues[property];
                    }

                    msg += Environment.NewLine + "property: " + property + Environment.NewLine;
                    msg += "\tproposedValue: " + proposedValue + Environment.NewLine;
                    msg += "\tdatabaseValue: " + databaseValue + Environment.NewLine;
                    logger.LogError(msg);
                    // TODO: decide which value should be written to database
                    // proposedValues[property] = <value to be saved>;
                }
                if (databaseValues != null)
                {
                    entry.OriginalValues.SetValues(databaseValues);
                }
                var e = new NotSupportedException(
                    "(Codepoint 3) Don't know how to handle concurrency conflicts for "
                    + entry.Metadata.Name + ": " + msg);
                throw e;
            }
            catch (Exception exception)
            {
                var e = new NotSupportedException(
                    "(Codepoint 2) Don't know how to handle concurrency conflicts for "
                    + entry.Metadata.Name + ": " + msg, exception);
                throw e;
            }
        }
Esempio n. 3
0
        public static void DbUpdateExceptionHandler(Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry entry, ILogger logger)
        {
            var    proposedValues = entry.CurrentValues;
            var    databaseValues = entry.GetDatabaseValues();
            var    setvalues      = databaseValues;
            string msg            = string.Empty;

            //ResultBase res;
            try
            {
                bool throwexception = false;
                foreach (var property in proposedValues.Properties)
                {
                    object proposedValue = null;
                    object databaseValue = null;
                    if (proposedValues.Properties?.Contains(property) == true)
                    {
                        proposedValue = proposedValues[property];
                    }
                    if (databaseValues == null)
                    {
                        setvalues = databaseValues;
                    }
                    if (databaseValues?.Properties?.Contains(property) == true)
                    {
                        databaseValue = databaseValues[property];
                    }

                    msg += "\r\n" + "property: \"" + property + "\"" + "\r\n";
                    msg += $"\tproposedValue: \"{proposedValue}\"\r\n";
                    msg += $"\tdatabaseValue: \"{databaseValue}\"\r\n";
                    if ((databaseValues != null) && ((databaseValue == null) ||
                                                     (String.IsNullOrEmpty(databaseValue.ToString()))))
                    {
                        setvalues           = databaseValues;
                        setvalues[property] = proposedValue;
                        msg += $"\tSetting databaseValue to proposed Value: \"{proposedValue}\"\r\n";
                        logger.LogDebug(msg);
                    }
                    else if (databaseValues == null)
                    {
                        setvalues           = proposedValues;
                        setvalues[property] = proposedValue;
                        msg += $"\tSetting databaseValue to proposed Values (since they are null): \"{proposedValue}\"\r\n";
                        logger.LogDebug(msg);
                    }
                    else
                    {
                        throwexception = true;
                        logger.LogError(msg);
                    }

                    // TODO: decide which value should be written to database
                    // proposedValues[property] = <value to be saved>;
                }
                //if (databaseValues != null)
                entry.OriginalValues.SetValues(setvalues);
                if (throwexception)
                {
                    var e = new NotSupportedException(
                        "(Codepoint 3) Don't know how to handle concurrency conflicts for "
                        + entry.Metadata.Name + ": " + msg);
                    throw e;
                }
            }
            catch (Exception exception)
            {
                var e = new NotSupportedException(
                    "(Codepoint 2) Don't know how to handle concurrency conflicts for "
                    + entry.Metadata.Name + ": " + msg, exception);
                throw e;
            }
        }