2017-08-28 14 views
1

エラー:「TenantUnits」テーブルでFOREIGN KEY制約「FK_dbo.TenantUnits_dbo.Units_Unit_Id」を導入すると、サイクルまたは複数のカスケードパスが発生することがあります。 NO DELETE NO ACTIONまたはUP UP NO NO ACTIONを指定するか、他のFOREIGN KEY制約を変更してください。Entity Framework多対多カスケード制約

私はこのエラーが私のモデルの関係の性質と関係があることを知っていますが、私はそれを整理するのはあまりにも混乱しています。私はカスケードの周りに私の心を包み込み、私のモデルに関わる多くの関係に多くの問題を抱えています。

モデルは以下の通りです:

public class Complex 
{ 
    [Key] 
    public Guid Id { get; set; } 
    public string Name { get; set; } 

    public Guid AddressId { get; set; } 

    [ForeignKey("AddressId")] 
    public virtual Address Address { get; set; } 

    public virtual ICollection<Unit> Units { get; set; } 

    public Complex() 
    { 
     this.Id = System.Guid.NewGuid(); 
     this.Units = new HashSet<Unit>(); 
    } 

    public void AddUnit(Unit unit) 
    { 
     Units.Add(unit); 
    } 
} 

public class Unit 
{ 
    [Key] 
    public Guid Id { get; set; } 
    public string Name { get; set; } 

    public Guid ComplexId { get; set; } 
    [ForeignKey("ComplexId")] 
    public virtual Complex Complex { get; set; } 

    public virtual ICollection<Tenant> Tenants { get; set; } 

    public Unit() 
    { 
     this.Id = System.Guid.NewGuid(); 
     this.Tenants = new HashSet<Tenant>(); 
    } 

    public void AddTenant(Tenant tenant) 
    { 
     Tenants.Add(tenant); 
    } 
} 

public class Tenant 
{ 
    [Key] 
    public Guid Id { get; set; } 
    public string UserId { get; set; } 
    public string FirstName { get; set; } 
    public string LastName { get; set; } 

    public Guid ContactInfoId { get; set; } 
    [ForeignKey("ContactInfoId")] 
    public ContactInfo ContactInfo { get; set; } 

    public virtual ICollection<Unit> Units { get; set; } 

    public Tenant() 
    { 
     this.Id = System.Guid.NewGuid(); 
     this.Units = new HashSet<Unit>(); 
    } 
} 

public class Address 
{ 
    [Key] 
    public Guid Id { get; set; } 
    public string Address1 { get; set; } 
    public string Address2 { get; set; } 
    public string City { get; set; } 
    public string State { get; set; } 
    public string Zip { get; set; } 

    public Address() 
    { 
     this.Id = System.Guid.NewGuid(); 
    } 
} 

public class ContactInfo 
{ 
    [Key] 
    public Guid Id { get; set; } 

    public Guid AddressId { get; set; } 
    [ForeignKey("AddressId")] 
    public Address Address { get; set; } 
    public string Phone1 { get; set; } 
    public string Phone2 { get; set; } 
    public string Email { get; set; } 

    public ContactInfo() 
    { 
     this.Id = System.Guid.NewGuid(); 
    } 
} 

編集:私はmodelBuilder.Conventions.Remove<ManyToManyCascadeDeleteConvention>(); を追加することで、エラーを解決しますが、私はまだ完全に効果を理解していないおよび/またはどのように働いている - あるいは、これはさえ何私の場合使用する必要があります。

答えて

0

ICollectionは、エンティティ間の1対多または多対多の関係を定義するために使用されます。あなたの "Complex"クラスでは、 "Unit"のICollectionを定義しています。しかし、Unitエンティティの主キーにマッピングされるComplexクラスで定義された外部キープロパティ "UnitId"はありません。 Entity Frameworkは、Complexクラスの "Id"をUnitクラスの "Id"にマッピングします。 (仮定:ユニット、コンプレックス、テナントのIDは異なります)。これは、エラーの背後にある理由かもしれません。 "UnitId"プロパティを定義し、 "Units"コレクションに外部キー属性を追加します。同様に、テナントとユニットのクラスを修正します。