2013-10-15 38 views
29

我收到此錯誤引進國外KEY約束可能會導致循環或多個級聯路徑

表「區域」可能會導致 循環或多個級聯路徑引進國外KEY約束 「FK_dbo.Regions_dbo.Countries_CountryId」 。指定ON DELETE NO ACTION或ON UPDATE NO ACTION,或修改其他外鍵約束。無法 創建約束。查看以前的錯誤。

我想知道這是不是意味着我的數據庫設計是壞?我讀您關閉級聯或類似的東西,但我只是不知道這是席捲問題出來了地毯的。

我只是讓EF在我的領域類生成我的表(我沒有使用任何數據註釋或流暢映射在這一點)。

 public class Country 
     { 
      public Country() 
      { 
       this.Stores = new List<Store>(); 
       this.Regions = new List<Region>(); 
       Id = GuidCombGenerator.GenerateComb(); 
      } 

      public Guid Id { get; private set; } 

      private string name; 

      public string Name 
      { 
       get { return name; } 
       set 
       { 
        name = value.Trim(); 
       } 
      } 

      private string code; 

      public string Code 
      { 
       get { return code; } 
       set 
       { 
        code = value.Trim(); 
       } 
      } 

      public virtual ICollection<Store> Stores { get; set; } 
      public virtual ICollection<Region> Regions { get; set; } 
     } 


      public class City 
     { 
      public City() 
      { 
       this.Stores = new List<Store>(); 
       Id = GuidCombGenerator.GenerateComb(); 
      } 

      public Guid Id { get; private set; } 

      private string name; 

      public string Name 
      { 
       get { return name; } 
       set 
       { 
        name = value.Trim(); 
       } 
      } 


      public Guid RegionId { get; set; } 
      public virtual Region Region { get; set; } 

      public virtual ICollection<Store> Stores { get; set; } 
     } 


      public class Region 
     { 
      public Region() 
      { 
       this.Cities = new List<City>(); 
       this.Stores = new List<Store>(); 


       Id = GuidCombGenerator.GenerateComb(); 
      } 

      public Guid Id { get; private set; } 


      private string state; 

      public string State 
      { 
       get { return state; } 
       set 
       { 
        state = value.Trim(); 
       } 
      } 


      public Guid CountryId { get; set; } 
      public virtual ICollection<City> Cities { get; set; } 
      public virtual Country Country { get; set; } 
      public virtual ICollection<Store> Stores { get; set; } 
     } 


    public class Store 
    { 
     public Store() 
     { 
      Id = GuidCombGenerator.GenerateComb(); 

      Users = new List<User>(); 
     } 

     public Guid Id { get; private set; } 

     public Guid CountryId { get; set; } 
     public Guid CityId { get; set; } 
     public Guid RegionId { get; set; } 
     public virtual City City { get; set; } 
     public virtual Country Country { get; set; } 
     public virtual Region Region { get; set; } 

     public virtual ICollection<User> Users { get; set; } 

    } 

難道是因爲商店?

+0

,你能否告訴我們涉及到的表,其結構是什麼,最重要的是:如何FK約束在它們之間建立?所以你想在區域和國家之間建立一個級聯刪除,這樣如果一個國家被刪除,它的所有區域都會被刪除?聽起來很合理 - 問題是:爲什麼會造成循環?你已經有了什麼其他的FK級聯刪除約束? –

+0

好的,我更新它來顯示這些區域,我沒有任何生成的表,因爲我正在做代碼第一。 – chobo2

+0

不幸的是,您沒有向我們展示'Store'類....並且我沒有看到任何定義級聯刪除的代碼,或者... –

回答

54

模型中的所有關係需要因爲所有的外鍵的屬性(CountryIdRegionIdCityId)是不可爲空。對於需要一個一對多的關係EF將使級聯按約定刪除。

CountryRegion有多個刪除路徑的Store表,例如,如果你刪除一個Country相關Store S可通過三種不同的層疊路徑(這是不允許使用SQL Server)中刪除:

  • Country - >Store
  • Country - >Region - >Store
  • Country - >Region - >City - >Store

你必須避免這種曖昧刪除路徑由要麼禁用級聯刪除使用流利的API或通過定義某些關係作爲可選(用空的外鍵Guid?)。

或從City以外的所有實體中刪除Stores集合(以及反向引用和FK屬性)。對我來說,這些集合看起來多餘,因爲您可以通過瀏覽Regions.Cities.Stores集合找到Country中的所有商店。

+0

所以商店只會與城市有關係? – chobo2

+0

@ chobo2:是的,我認爲這就足夠了。 – Slauma

+12

+1內容豐富;呃,這個SQL Server部分的限制令人討厭。在許多情況下,您都有一個表在兩個表之間實現多對多關係,並且您希望在關係表中的這兩個表中都有不可空的外鍵,並且可以刪除任何一個這些表級聯到關係表。所以我想我們留下了一個非理想的解決方案,讓其中一個外鍵可以爲空。要麼是每次都要實施自定義觸發器。什麼皮塔餅。 – Shavais

24

OnModelCreating方法添加modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>()DataContext文件如下:

public class YourDataContext : DbContext 
{ 
    public DbSet<Country> Countries{ get; set; } 
    ... 


    protected override void OnModelCreating(DbModelBuilder modelBuilder) 
    { 
     modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>(); 

    } 
} 

同樣的問題:entity-framework-how-to-solve-foreign-key-constraint-may-cause-cycles-or-multi

+18

這不是一個解決方案,即解決這個問題。 – Sebazzz

+4

當您對模式設計沒有任何控制權時,這是確切的解決方案。 – Askolein

+0

so·lu·tion səlo͞oSH(ə)n/ noun 1.一種解決問題或處理困難情況的方法。 –

相關問題