public static ConfigStoreError ToError(Exception ex)
        {
            ConfigStoreException ce = ex as ConfigStoreException;

            if (ce != null)
            {
                return(ce.Error);
            }

            ChangeConflictException conflict = ex as ChangeConflictException;

            if (conflict != null)
            {
                return(ConfigStoreError.Conflict);
            }

            SqlException sqlex = ex as SqlException;

            if (sqlex != null)
            {
                ConfigStoreError errorCode = ConfigStoreError.DatabaseError;
                switch (sqlex.Number)
                {
                default:
                    break;

                case (int)SqlErrorCodes.DuplicatePrimaryKey:
                case (int)SqlErrorCodes.UniqueConstraintViolation:
                    errorCode = ConfigStoreError.UniqueConstraint;
                    break;

                case (int)SqlErrorCodes.ForeignKeyViolation:
                    errorCode = ConfigStoreError.ForeignKeyConstraint;
                    break;
                }

                return(errorCode);
            }

            DbException dbex = ex as DbException;

            if (dbex != null)
            {
                return(ConfigStoreError.DatabaseError);
            }

            return(ConfigStoreError.Unknown);
        }
        public static string SerializeForLog(this ChangeConflictException e, DataContext context)
        {
            StringBuilder builder = new StringBuilder();

            using (StringWriter sw = new StringWriter(builder))
            {
                sw.WriteLine("Optimistic concurrency error:");
                sw.WriteLine(e.Message);

                foreach (ObjectChangeConflict occ in context.ChangeConflicts)
                {
                    Type      objType          = occ.Object.GetType();
                    MetaTable metatable        = context.Mapping.GetTable(objType);
                    object    entityInConflict = occ.Object;

                    sw.WriteLine("Table name: {0}", metatable.TableName);

                    var noConflicts =
                        from property in objType.GetProperties(BindingFlags.Public | BindingFlags.Instance)
                        where property.CanRead &&
                        property.CanWrite &&
                        property.GetIndexParameters().Length == 0 &&
                        !occ.MemberConflicts.Any(c => c.Member.Name != property.Name)
                        orderby property.Name
                        select property;

                    foreach (var property in noConflicts)
                    {
                        sw.WriteLine("\tMember: {0}", property.Name);
                        sw.WriteLine("\t\tCurrent value: {0}",
                                     property.GetGetMethod().Invoke(occ.Object, new object[0]));
                    }

                    sw.WriteLine("\t-- Conflicts Start Here --");

                    foreach (MemberChangeConflict mcc in occ.MemberConflicts)
                    {
                        sw.WriteLine("\tMember: {0}", mcc.Member.Name);
                        sw.WriteLine("\t\tCurrent value: {0}", mcc.CurrentValue);
                        sw.WriteLine("\t\tOriginal value: {0}", mcc.OriginalValue);
                        sw.WriteLine("\t\tDatabase value: {0}", mcc.DatabaseValue);
                    }

                    sw.WriteLine("\t-- Conflicts End --");
                }

                sw.WriteLine();
                sw.WriteLine("Attempted SQL: ");

                TextWriter tw = context.Log;

                try
                {
                    context.Log = sw;
                    context.SubmitChanges();
                }
                catch (ChangeConflictException)
                {
                }
                catch
                {
                    sw.WriteLine("Unable to recreate SQL!");
                }
                finally
                {
                    context.Log = tw;
                }

                sw.WriteLine();
            }

            return(builder.ToString());
        }