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

Entity child collection update #2999


User avatar
0
Ricavir created

Hi,

I have an issue when trying to update a collection inside an Entity. My entities are :

Address entity :

[Table("LsAddresses")]
    public class Address : FullAuditedEntity<long>, IMustHaveTenant
    {               
        public virtual int TenantId { get; set; }

        [Required]
        [MaxLength(ConstForLength.Name)]
        public virtual string Name { get; set; }
        
        [MaxLength(ConstForLength.DescriptionTitle)]
        public virtual string Title { get; set; }        

        public virtual ICollection<PhoneAddress> Phones { get; set; }
    }

PhoneAddress entity :

[Table("LsPhonesAddress")]
    public class PhoneAddress : FullAuditedEntity<long>
    {
        [ForeignKey("AddressId")]
        public virtual Address Address { get; set; }
        public virtual long AddressId { get; set; }

        [Required]
        [MaxLength(ConstForLength.Number)]
        public virtual string Number { get; set; }
    }

I have created an interface with following DTO

[AutoMap(typeof(Address))]
    public class AddressEditDto : EntityDto<long>
    {
        [Required]
        [MaxLength(ConstForLength.Name)]
        public string Name { get; set; }

        [MaxLength(ConstForLength.DescriptionTitle)]
        public string Title { get; set; }
       
        public Collection<PhoneAddressInAddressListDto> Phones { get; set; }
    }

  [AutoMap(typeof(PhoneAddress))]
    public class PhoneAddressInAddressListDto
    {       
        public string Number { get; set; }
    }

And my application service :

[AbpAuthorize(AppPermissions.Pages_Tenant_Addresses_Create)]
        public async Task CreateAddress(AddressEditDto input)
        {
            var address = input.MapTo<Address>();
            await _addressRepository.InsertAsync(address);
            
        }

        [AbpAuthorize(AppPermissions.Pages_Tenant_Addresses_Edit)]
        public async Task<AddressEditDto> GetAddressEdit(EntityDto<long> input)
        {
            var address = await _addressRepository.GetAsync(input.Id);
            return address.MapTo<AddressEditDto>();
        }

        [AbpAuthorize(AppPermissions.Pages_Tenant_Addresses_Edit)]
        public async Task UpdateAddressEdit(AddressEditDto input)
        {
            var address = await _addressRepository.GetAsync(input.Id);
            input.MapTo(address);
            await _addressRepository.UpdateAsync(address);            
        }

In angular2 side, I'm able to create and get address entities with phones. But when I try to update an Address entity with a phone collection, I have foreign key exception on server side. Here is the exception from log :

System.InvalidOperationException: The operation failed: The relationship could not be changed because one or more of the foreign-key properties is non-nullable. When a change is made to a relationship, the related foreign-key property is set to a null value. If the foreign-key does not support null values, a new relationship must be defined, the foreign-key property must be assigned another non-null value, or the unrelated object must be deleted. à System.Data.Entity.Core.Objects.ObjectContext.PrepareToSaveChanges(SaveOptions options) à System.Data.Entity.Core.Objects.ObjectContext.<SaveChangesInternalAsync>d__31.MoveNext()

I could remove collection from the DTO and have separated updates but I would like to fin a clean way to update collection inside an entity for other cases.

Any help is welcome ;)


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

    Hi,

    I could remove collection from the DTO and have separated updates but I would like to fin a clean way to update collection inside an entity for other cases.
    

    I think you should do it in this way because as far as I know EF 6.x does not support it.

  • User Avatar
    0
    Ricavir created

    Thks for the hint. I will do so ;)