繁体   English   中英

由于一个或多个外键属性不可为空,因此无法更改该关系

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

例外情况:

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.

我已经看到了上述问题的一些解决方案,如下所示,但是它们都不适合我:(可能是由于这些解决方案适用于EF 4.x版本。我的应用程序的EF版本是6.x.

解决方案1解决方案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; }

    }

正如我上面提到的,我在我的应用程序中使用存储库的位置,那么你能告诉我如何解决以上问题吗? 谢谢。

注意:我可以将记录添加到db.But尝试执行更新时会出现问题。

图像表示:

1:M

img1

当您点击上方的“ Lot Info按钮时,就会出现这种情况。

在此处输入图片说明

注意:有2个表,一个是Properties ,另一个是TaxMapLots关系是1 : M 用户可以在TaxMapLot formadd/edit or delete记录。

更新:实际上我可以添加一条记录。尝试编辑记录时出现问题。

可以正常工作( CreatePropertyAsync ):将记录添加到表(Properties和TaxMapLots)中。问题出在如上所示的Edit方法上。

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

发生这种情况的原因是,当您将dto映射到实体时,由于dto包含完整图形,因此先前的相关TaxMapLot已从TaxMapLots集合中删除。

EF认为您的收藏品是新收藏品,而先前的收藏品已被删除,因此它们的PropertyIds为空。 您需要自己处理更新TaxMapLot,而不是让mapper为您完成。

使数据库表IpTaxMapLots和TaxMapLot实体中的外键PropertyId都为空:

public virtual int? PropertyId { get; set; }

当然,您需要考虑对于您的业务来说有意义的是没有属性的TaxMapLot。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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