簡體   English   中英

“該關聯已被切斷,但該關系要么被標記為‘必需’,要么被隱含地要求......”

[英]"The association has been severed but the relationship is either marked as 'Required' or is implicitly required..."

嘗試添加遷移時出現以下錯誤:

PS C:\Code\morpher.ru\Morpher.Database> dotnet ef migrations add AddQazaqFeatures --startup-project=../Morpher.Database.Design
Build started...
Build succeeded.
System.InvalidOperationException: The association between entity types 'Service' and 'Deployment' has been severed but the relationship is either m
arked as 'Required' or is implicitly required because the foreign key is not nullable. If the dependent/child entity should be deleted when a requi
red relationship is severed, then setup the relationship to use cascade deletes.  Consider using 'DbContextOptionsBuilder.EnableSensitiveDataLoggin
g' to see the key values.
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry.HandleConceptualNulls(Boolean sensitiveLoggingEnabled, Boolean forc
e, Boolean isCascadeDelete)
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.CascadeDelete(InternalEntityEntry entry, Boolean force, IEnumerable`1 fore
ignKeys)
 ...

我的代碼:

    public class Deployment
    {
        public int Id { get; set; }
        public virtual Service Service { get; set; }
        public int ServiceId { get; set; }
        
        public string Host { get; set; }
        public short? Port { get; set; }
        public string BasePath { get; set; }
    }

    public class Service
    {
        public int Id { get; set; }
        public string Name { get; set; }
        
        public string UrlSlug { get; set; }
        
        public virtual ICollection<Endpoint> Endpoints { get; set; }
        
        public virtual ICollection<Deployment> Deployments { get; set; }
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Service>().HasData(new Service
        {
            Name = "Веб-сервис «Морфер»",
            UrlSlug = "ws",
            Id = 1
        });
        modelBuilder.Entity<Deployment>().HasData(new Deployment
        {
            Host = "ws3.morpher.ru",
            ServiceId = 1,
            Id = 1
        });

        modelBuilder.Entity<Deployment>(entity =>
        {
            entity.Property(e => e.Host).IsRequired().HasMaxLength(256);
            entity.Property(e => e.BasePath).HasMaxLength(512);

            entity.HasOne(deployment => deployment.Service)
                .WithMany(service => service.Deployments)
                .HasForeignKey(d => d.ServiceId)
                .OnDelete(DeleteBehavior.Restrict)
                .HasConstraintName("FK_Deployments_Services");
        });
     }

有許多 StackOverflow 問題提到了相同的錯誤( 123 ),但它們主要與刪除實體有關,而沒有 CASCADE 刪除策略或可為空的外鍵。

就我而言,我正在嘗試添加新行,但我不明白為什么它正在考慮“切斷”關系。 設置 ServiceId = 1 還不夠嗎?

通過首先刪除 model 數據種子代碼( HasData調用),然后添加遷移以創建表/關系,然后添加數據種子代碼並嘗試添加新的遷移。

它不會在最新的 EF Core 6.0 中發生,因此顯然您遇到了 EF Core 3.1 缺陷/錯誤,該缺陷/錯誤已在路上某處修復。 因此,您要么需要升級到較新的 EF Core 版本(承擔所有相關的負擔,例如重新測試所有內容、中斷更改等),要么使用下面的解決方法。

解決方法是將DeleteBehavior.Restrict替換為ClientNoActionNoAction 該枚舉的值和它們所做的文檔有點混亂,但是所有這 3 個值似乎在數據庫中生成了一個相同的常規強制 FK 約束(沒有級聯),並且僅在客戶端行為或在換句話說,EF Core 更改跟蹤器在“刪除”主體實體時對相關的被跟蹤實體做了什么。 在這種特殊情況下,當有跟蹤(加載)的相關實體實例時,“限制”會拋出異常,而其他兩個不會。

我知道您認為您只是在“添加數據”,但 EF Core model 數據播種不止於此 - 它試圖保留該數據,因此在某些情況下它需要更新或刪除以前添加的數據。 這通常有效,除非 EF Core 代碼庫中存在錯誤,例如在這種情況下。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM