2017-02-27 16 views
1

2つの列を持つ表があり、2つの列には1つの表の参照があります。Entity Framework 2多対1の列

最初にEntity Frameworkコードを使用していますが、移行を構築しようとすると、私が持っているアプローチは機能しません。

"親":

public class NodesTree 
{ 
    public NodesTree(){ 
     this.ActualTreeNode = new List<TreePath>(); 
     this.NextTreeNode = new List<TreePath>(); 
    } 

    public int TreeId { get; set; } 

    public virtual Tree Tree { get; set; } 

    public virtual ICollection<TreePath> ActualTreeNode { get; set; } 

    public virtual ICollection<TreePath> NextTreeNode { get; set; } 
} 

子:

public class TreePath 
{ 
    public TreePath() 
    { 
    } 

    public int NodeId { get; set; } 

    public virtual NodesTree Node { get; set; } 

    public int NextNodeId { get; set; } 

    public virtual NodesTree NextNode { get; set; } 

    public int TreeId { get; set; } 

    public virtual Tree Tree { get; set; } 
} 

設定私はこれらの定義を持っている:

 this.HasRequired(n => n.Node) 
      .WithMany(t => t.ActualTreeNode) 
      .HasForeignKey(n => n.NodeId) 
      .WillCascadeOnDelete(false); 

     this.HasRequired(n => n.NextNode) 
      .WithMany(t => t.NextTreeNode) 
      .HasForeignKey(n => n.NextNodeId) 
      .WillCascadeOnDelete(false); 

私はこのエラーを得たの移行を追加する場合:

The number of properties in the Dependent and Principal Roles in a relationship constraint must be identical.

答えて

1

エラーの原因は、モデルに不適切な構成の関係があるためです。これは正しくありません:

this.HasRequired(n => n.Node) 
    .WithMany(t => t.ActualTreeNode) 
    .HasForeignKey(n => n.NodeId) 
    .WillCascadeOnDelete(false); 

this.HasRequired(n => n.NextNode) 
    .WithMany(t => t.NextTreeNode) 
    .HasForeignKey(n => n.NextNodeId) 
    .WillCascadeOnDelete(false); 

それは次のようになります。

this.HasRequired(n => n.Node) 
    .WithMany(t => t.ActualTreeNode) 
    .HasForeignKey(n => new { n.NodeId, n.NextNodeId }) 
    .WillCascadeOnDelete(false); 

の依存FKは元本PKのすべての列が含まれている必要がありますので。また、ナビゲーションプロパティをThree to Oneから削除する必要があります。

+0

まだ同じ問題があります。 – Blackout

関連する問題