Esempio n. 1
0
 void IUnitOfWork.SaveChanges()
 {
     try
     {
         Context.SaveChanges();
     }
     catch (DbEntityValidationException ex)
     {
         throw DbExceptionsConverter.Convert(ex);
     }
     catch (DbUpdateException ex)
     {
         throw DbExceptionsConverter.Convert(ex);
     }
 }
        protected virtual void RemoveCore(TEntity entity)
        {
            try
            {
                //DbSet.Remove will clear nullable Guid value. So remember it and restore it later
                var sourceProperties = typeof(TEntity)
                                       .GetProperties()
                                       .Where(p => p.CanRead && p.CanWrite &&
                                              p.GetCustomAttributes(typeof(System.ComponentModel.DataAnnotations.Schema.NotMappedAttribute), true).Length == 0);
                var notVirtualProperties = sourceProperties.Where(p => !p.GetGetMethod().IsVirtual);

                List <KeyValuePair <PropertyInfo, Guid?> > recordKeys = new List <KeyValuePair <PropertyInfo, Guid?> >();
                foreach (var nullableGUIDProperty in notVirtualProperties)
                {
                    if (nullableGUIDProperty.PropertyType == typeof(Guid?))
                    {
                        recordKeys.Add(new KeyValuePair <PropertyInfo, Guid?>(nullableGUIDProperty, (Guid?)nullableGUIDProperty.GetValue(entity)));
                    }
                }

                DbSet.Remove(entity);

                foreach (var recordKey in recordKeys)
                {
                    recordKey.Key.SetValue(entity, recordKey.Value);
                }
            }
            catch (DbEntityValidationException ex)
            {
                throw DbExceptionsConverter.Convert(ex);
            }
            catch (DbUpdateException ex)
            {
                throw DbExceptionsConverter.Convert(ex);
            }
        }