簡體   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