2017-02-13 1 views
0

私のデータベースが設定されているため(私は制御できません)、Entityで把握できない外部キー状況がありますフレームワーク。私はFluent APIを使って私の関係を設定しています。私のクラスは、Entity Frameworkの関係は2つのテーブルのエンティティように、私は流暢API外部キーマッピングを設定するにはどうすればよい非標準の非一意の外部キー

SELECT * 
FROM Parent 
JOIN Child 
ON Child.ChildForeignKey1 = Parent.ParentForeignKey1 
AND Child.ChildForeignKey2 = Parent.ParentForeignKey2 

ようParentForeignKeyChildForeignKey分野に参加するようなものであるべきである

public class Parent 
{ 
    // Unique primary key 
    public int PrimaryKey { get; set; } 

    // These two fields together compose the foreign key to join to Child 
    public string ParentForeignKey1 { get; set; } 
    public string ParentForeignKey2 { get; set; } 

    public List<Child> Children { get; set; } 
} 

public class Child 
{ 
    // Unique primary key 
    public int PrimaryKey { get; set; } 

    // These two fields together compose the (non-unique) foreign key to join to Parent 
    public string ChildForeignKey1 { get; set; } 
    public string ChildForeignKey2 { get; set; } 

    // The parent/child relationship is implicit, even though the tables 
    // themselves indicate a many-to-many relationship 
    public List<Parent> Parents { get; set; } 
} 

ですフレームワークはDbSetを照会するとこれらの結合を生成しますか?

+0

を作ると思います。コード内にリストがあるはずはありませんか? – jdweng

+0

良い点。私は質問を更新します。私は、親子関係が暗黙的であるため(SQL結合が実際には多対多であっても、子ごとに1つの親しか存在しないため)、それを入れませんでした。 –

答えて

0

私は、これはあなたが唯一の親を持っているより多くの意味

public class Parent 
{ 
    // The parent/child relationship is implicit, even though the tables 
    // themselves indicate a many-to-many relationship 
    public static List<Parent> Parents { get; set; } 


    // Unique primary key 
    public int PrimaryKey { get; set; } 

    // These two fields together compose the foreign key to join to Child 
    public string ParentForeignKey1 { get; set; } 
    public string ParentForeignKey2 { get; set; } 

    public List<Child> Children { get; set; } 
} 

public class Child 
{ 
    // Unique primary key 
    public int PrimaryKey { get; set; } 

    // These two fields together compose the (non-unique) foreign key to join to Parent 
    public string ChildForeignKey1 { get; set; } 
    public string ChildForeignKey2 { get; set; } 

} 
+0

'Parent'にstaticプロパティを置くのは何ですか? –

+0

親クラスのすべてのインスタンスに対して1つの共通List <>オブジェクトを作成します。 2つのクラスは最終結果を表します。子クラスのインプットはどこから来ますか? ParentクラスのChildrenはリンクですが、ChildのList <>の入力はありません。私は静的な子供リストも必要と思う<> – jdweng

+0

私は私の元の質問で不明だったかもしれません。 Entity Frameworkが 'Parent'を' Child'に(LINQ文から)結合させるためにSQLを生成するときに、テーブルを結合するように、外部キーの関係( '... HasForeignKey()...')を設定する方法を知りたい私の例のように。 –

関連する問題