Base solution for your next web application
Starts in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

Simply soft-delete entity #2334


User avatar
0
alpiqintec created

Hi,

I would like to know how can I decode the famous exception: System.Data.Entity.Validation.DbEntityValidationException: Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.

with no details about what it is wrong with a simple soft-delete of an weak Entity with a relation to another Entity. What can go wrong in this case? I already try to override the SaveChanges() and use a try/catch to look at the details in the DbContext but with no luck.

Thanks DP


7 Answer(s)
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    Have you checked Logs.txt file under your web project ? It might have more detailed error message.

  • User Avatar
    0
    alpiqintec created

    Nope, there is nothing useful in the Logs.txt, only:

    Handling.AbpApiExceptionFilterAttribute - Validation failed for one or more entities. See 'EntityValidationErrors' property for more details. System.Data.Entity.Validation.DbEntityValidationException: Validation failed for one or more entities. See 'EntityValidationErrors' property for more details. at System.Data.Entity.Internal.InternalContext.SaveChangesAsync(CancellationToken cancellationToken) at System.Data.Entity.Internal.LazyInternalContext.SaveChangesAsync(CancellationToken cancellationToken) at Abp.EntityFramework.AbpDbContext.<SaveChangesAsync>d__38.MoveNext() in D:\Halil\GitHub\aspnetboilerplate\src\Abp.EntityFramework\EntityFramework\AbpDbContext.cs:line 208 --- End of stack trace from previous location where exception was thrown ---

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    Can you share your Entity with us ?

  • User Avatar
    0
    meff created

    Hello. Simply override SaveChanges in your DbContext class:

    public override int SaveChanges()
            {
                try
                {
                    return base.SaveChanges();
                }
                catch (DbEntityValidationException ex)
                {
                    var errorMessages = ex
                        .EntityValidationErrors
                        .SelectMany(x => x.ValidationErrors)
                        .Select(x => x.ErrorMessage);
                    var fullError = string.Join(Environment.NewLine, errorMessages);
    
                    var exceptionMessage = string.Concat(ex.Message, Environment.NewLine, "Validations errors are:", Environment.NewLine, fullError);
    
                    throw new DbEntityValidationException(exceptionMessage, ex.EntityValidationErrors);
                }
    
            }
    
  • User Avatar
    0
    alpiqintec created

    Hi, I already overrided the SaveChanges() Method, there is nothing useful there.

    May entities are the following:

    [Table("MyObjects")] public class MyObject : FullAuditedEntity, IMustHaveTenant { [Required] public virtual string Name { get; set; }

    [Required]  //->with this I am not able to soft-delete
    public virtual MyObjectType Type { get; set; }
    
    public virtual MyObject Parent { get; set; }
    

    }

    [Table("MyObjectTypes")] public class MyObjectType : FullAuditedEntity, IMustHaveTenant { [Required] public virtual string Description { get; set; }

    public virtual bool AllowChildNodes { get; set; }
    
    public virtual bool IsDefault { get; set; }
    

    }

    I am only able to soft-delete the entries of MyObject when the property Type (MyObjectType ) is not required. Why is this happening?

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    Can you share the delete code as well ? Including how do you get this record from db. We will try to reproduce and fix it.

    Thanks.

  • User Avatar
    0
    alpiqintec created

    Hi,

    I tried both:

            var Object = await _objectRepository.GetAsync(input.Id);
            await _objectRepository.DeleteAsync(Object);
    

    and: await _objectRepository.DeleteAsync(input.Id);

    but I have the Validation Error.

    Thank you