Base solution for your next web application
Open Closed

Strange validation error #3173


User avatar
0
vnetonline created

I am getting a very strange error. My solution was working correctly.

This is the method signature of my personAppService.

public PersonAppService : AppConnectAppServiceBase, IPersonAppService { public async Task CreateOrUpdateApplicant(OppPersonInput input, string opportunityId); }

I call this method from within another service like so,

await _personAppService.CreateOrUpdateApplicant(input.Applicant, input.OpportunityId);

My input objects looks like this public class input { public string OpportunityId { get; set; } public OppPersonInput Applicant { get; set; } }

[AutoMap(typeof(Person))] public class OppPersonInput { [MaxLength(40)] public string LastName { get; set; } [MaxLength(40)] public string FirstName { get; set; } }

Person is an entity framework entity.

When input.Applicant is passed as null from the DTO, the CreateOrUpdateApplicant throws a validation exception ( the control does not even pass into the function). I have also tried passing null directly into this function but that doesnt work either. OppPersonInput has not been set to [Required] and none of its properties are Required.

I have made sure that I am passing {Applicant : null} in the input object DTO through swagger. But even if I pass a null value to the function directly as in _personAppService.CreateOrUpdateApplicant(null, input.OpportunityId) I get the validation messages below (in C# obviously you can pass null to a function).

'Abp.Runtime.Validation.Abp ValidationException' in Abp.dll ("Method arguments are not valid! See Validation Errors for details.")

See ValidationErrors for details. at Abp.Runtime.Validation.Interception.MethodInvocationValidator.Validate() in D:\Halil\Github\aspnetboilerplate\src\Abp\Runtime\Validation\Interception\MethodInvocationValidator.cs:line 94 at Abp.Runtime.Validation.Interception.ValidationInterceptor.Intercept(IInvocation invocation) in D:\Halil\Github\aspnetboilerplate\src\Abp\Runtime\Validation\Interception\ValidationInterceptor.cs:line 31 at Castle.DynamicProxy.AbstractInvocation.Proceed()


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

    Hi,

    ABP does not allow null values as far as I remember. You can try to disable validation and run this code again to see if it works. <a class="postlink" href="https://aspnetboilerplate.com/Pages/Documents/Validating-Data-Transfer-Objects#disabling-validation">https://aspnetboilerplate.com/Pages/Doc ... validation</a>

  • User Avatar
    0
    vnetonline created

    Thanks for your response. I actually fixed this by doing the following.

    await _personAppService.CreateOrUpdateApplicant( input.OpportunityId,input.Applicant = null);

    However there are some other errors I am getting. One of these is that two DTO objects cannot be passed into the same Service method. This I resolved by using another object that is a composite of these objects.

    However, the other one is very weird. We have a Leads table and delete async is not working. It says Brand Id and Branch Id are required. These are required fields in the Leads table but there should not be any problems with Delete. Following is the function call.

        public async Task DeleteLead(EntityDto&lt;string&gt; input)
        {
            await _leadRepository.DeleteAsync(input.Id);
        }
    

    This link had similar problems but no solution posted there.

    #2334

    These is the error message in detail

    ERROR 2017-06-01 15:11:26,459 [6 ] nect.EntityFramework.AppConnectDbContext - There are some validation errors while saving changes in EntityFramework: ERROR 2017-06-01 15:11:26,459 [6 ] nect.EntityFramework.AppConnectDbContext - - BranchId: The BranchId field is required. ERROR 2017-06-01 15:11:26,460 [6 ] nect.EntityFramework.AppConnectDbContext - - BrandId: The BrandId field is required. ERROR 2017-06-01 15:11:26,501 [6 ] nHandling.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.

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    One of these is that two DTO objects cannot be passed into the same Service method. This I resolved by using another object that is a composite of these objects.

    Actually, this is the offered way, so it is good to do it like this.

    For your second problem, I think your entity is a soft delete entity. In that case, ABP does not delete the entity but updates it's IsDeleted column to 1/true.

    But DeleteAsync method gets record from DB if it is not already attached to your DbContext. If the below code is your delete code, it is hard to cause this problem.

    public async Task DeleteLead(EntityDto<string> input)
    {
        await _leadRepository.DeleteAsync(input.Id);
    }
    

    you can try to delete your entity like this and see if it works in that way:

    public async Task DeleteLead(EntityDto<string> input)
    {
        var lead = await _leadRepository.GetAsync(input.Id);
        await _leadRepository.DeleteAsync(lead);
    }
    

    Thanks.

  • User Avatar
    0
    vnetonline created

    Yes tried that already but same error. It's happening for another table as well. Is there some way to debug this? Could it be that some packages are out of date and if so how should these packages be updated using nuget? Thanks.

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    Which version of ABP do you use in your project ? If you can share your entity definition, we can try to reproduce this problem.

    Thanks.

  • User Avatar
    0
    vnetonline created

    It appears that this happens when the foreign key is a string and is required. For eg

    [Required] public virtual string BranchId { get; set; }

    If the foreign keys are longs, ints etc, they can be non nullable. But in case of strings, this causes a problem. It would be nice if someone could investigate whether this is a boilerplate issue or an entity framework one.

    For the time being I simply do this to make it all work.. (let me know if this is wrong approach). It tends to update the LastModificationTime, DeletionTime and DeleterUserId correctly.

        public async Task Delete(EntityDto&lt;string&gt; input)
        {
            var lead = await _leadRepository.GetAsync(input.Id);
            if(lead != null)
            {
                lead.IsDeleted = true;
            }
        }
    
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    As I remember we made a change in ABP framework to prevent this error. If you update your ABP packages, this should be fixed.

    If you can share your ABP version, we can try to find this change.

    Thanks.