Base solution for your next web application
Open Closed

One or more of the foreign-key properties is non-nullable #778


User avatar
0
sampath created

Hi,

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.

I have seen some solutions for the above issue as shown below.But I don't know how to apply it with the ABP. Any help would be highly appreciated.Thanks.

[http://stackoverflow.com/questions/5538974/the-relationship-could-not-be-changed-because-one-or-more-of-the-foreign-key-pro])

[http://stackoverflow.com/questions/19325473/ef6-0-the-relationship-could-not-be-changed-because-one-or-more-of-the-foreign])

[Table("IpTaxMapLots")]
    public class TaxMapLot : FullAuditedEntity
    {
        public const int MaxLength = 50;

        [Required]
        [MaxLength(MaxLength)]
        public virtual string District { get; set; }

        [ForeignKey("PropertyId")]
        public virtual Property Property { get; set; }
        public virtual int PropertyId { get; set; }
    }
[Table("IpProperties")]
    public class Property : FullAuditedEntity
    {
        public const int MaxLength = 50;

        [MaxLength(MaxLength)]
        public virtual string Dist { get; set; }

        public virtual ICollection<TaxMapLot> TaxMapLots { get; set; }
    }
public async Task<int?> EditPropertyAsync(CreateOrEditPropertyInput input)
        {
            var property = await _propertyRepository.FirstOrDefaultAsync(p => p.Id == input.Property.Id);
            input.Property.MapTo(property);
          
            await _propertyRepository.UpdateAsync(property);
            return input.Property.Id;
        }
public class CreateOrEditPropertyInput : IInputDto
    {
        [Required]
        public PropertyEditDto Property { get; set; }
    }
[AutoMap(typeof(Property))]
    public class PropertyEditDto
    {
        public const int MaxLength = 50;
        public int? Id { get; set; }

        [MaxLength(MaxLength)]
        public string Dist { get; set; }

        public List<TaxMapLotDto> TaxMapLots { get; set; }

    }

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

    Hi,

    This is completely related to EntityFramework. There were similar problems on the forum, you can search for it. In brief, you can not just override a collection, you should manually insert/delete/modify subitems in a collection (navigation property). At least, I don't know a better way yet.

  • User Avatar
    0
    sampath created

    Hi, I have tried like this.But still the same error :( Do you know why ? Thanks.

    public async Task<int?> EditPropertyAsync(CreateOrEditPropertyInput input)
            {
                var property = await _propertyRepository.FirstOrDefaultAsync(p => p.Id == input.Property.Id);
                input.Property.MapTo(property);
              
                using (var unitOfWork = _unitOfWorkManager.Begin())
                {
                    foreach (var taxMapLot in property.TaxMapLots.ToList())
                    {
                        await _taxMapLotRepository.DeleteAsync(taxMapLot);
                    }
    
                    unitOfWork.Complete();
                }
                
                await _propertyRepository.UpdateAsync(property);
                return input.Property.Id;
            }
    

    Can you give code sample for this comment ?

    In brief, you can not just override a collection, you should manually insert/delete/modify subitems in a collection (navigation property). At least, I don't know a better way yet.

    Update : Actually I can Add a record.The problem occurs when I try to edit the record.

    This works fine (CreatePropertyAsync) : It adds record to both the table (Properties and TaxMapLots).The problem is on Edit method as shown above.

    public async Task<int> CreatePropertyAsync(CreateOrEditPropertyInput input)
            {
                var property = input.Property.MapTo<Property>();
                 var propertyId = await _propertyRepository.InsertAndGetIdAsync(property);
                return propertyId;
            }