简体   繁体   中英

The relationship could not be changed because one or more of the foreign-key properties is non-nullable

Exception :

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 none of them worked for me :( May be due to those solutions are for the EF 4.x versions.My app's EF version is 6.x.

Solution 1 and Solution 2

 [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);//issue is here
            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; }

    }

As I mentioned above Where I have used repositories on my app.So could you tell me how to sort out above issue ? Thanks.

Note : I can add records to the db.But problem occurs when I try to do the update.

Image representation :

1 : M

img1

This comes when you click the Lot Info Button above.

在此处输入图片说明

Note : There are 2 tables.One is Properties and other is TaxMapLots .The relationship is 1 : M . User can add/edit or delete records on the TaxMapLot form .

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 the 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;
        }

This happens because previous related TaxMapLot are being removed from the TaxMapLots collection when you are mapping dto to entity, as your dto contains full graph.

EF thinks your collection items are new ones and previous are being removed so theirs PropertyIds becomes null. You need to handle updating TaxMapLot by yourself instead of letting mapper do it for you.

Make foreign key PropertyId nullable in both, datatabase table IpTaxMapLots and TaxMapLot entity:

public virtual int? PropertyId { get; set; }

Of course you need to think if for your business make sense to have a TaxMapLot without a Property.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM