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

时间:2016-02-13 15:40:21

标签: c# entity-framework-6 repository-pattern

例外:

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. < / p>

Solution 1Solution 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; }

    }

正如我上面提到的那样我在app上使用了存储库。所以你能告诉我如何解决上述问题吗?谢谢。

注意:我可以将记录添加到db.But问题,当我尝试进行更新时。

图片展示

1:M

img1

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

enter image description here

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

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

这样可以正常工作(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;
        }

2 个答案:

答案 0 :(得分:0)

这是因为当您将dto映射到实体时,以前的相关TaxMapLot将从TaxMapLots集合中删除,因为您的dto包含完整图形。

EF认为您的收藏品是新的,之前的物品被删除,因此他们的PropertyIds变为空。 你需要自己处理更新TaxMapLot,而不是让mapper为你做。

答案 1 :(得分:0)

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

public virtual int? PropertyId { get; set; }

当然,您需要考虑如果您的企业有一个没有Property的TaxMapLot是有意义的。