2013-12-16 4 views

答えて

4

EFでは、結合テーブルはナビゲーションプロパティを使用して実行されます。基本的に、EFはあなたのためにそれを行います。リポジトリに実装する際にGenericであるかどうかを問わず、クエリー式を作成するときにIncludeメソッドを呼び出して、EFにナビゲーションプロパティを設定するように指示できます。

は、我々はこれらのPOCOクラスがあるとしましょう。ここでは

public class Dog 
{ 
    public int DogId { get; set; } 
    public string Name { get; set; } 

    public int OwnerId { get; set;} 
    public Owner Owner { get; set; } // the navigation property 
} 

public class Owner 
{ 
    public int OwnerId { get; set; } 
    public string Name { get; set; } 

    // another navigation property 
    // all the dogs that are related or owned by this specific owner 
    public ICollection<Dog> DogList { get; set; } 
    public ICollection<Cat> CatList { get; set; } 
} 

を含める使用してサンプルコードスニペットです:

public virtual IEnumerable<Dog> Retrieve() 
{ 
    var _query = context.Dog.Include(a => a.Owner); 
    ... 
    ...// rest of your code 
} 

そして、複数のテーブルのためにあなたがインクルードはそうのような方法巣含めることができます。

public virtual IEnumerable<Owner> Retrieve() 
{ 
    // you can nest as many as you want if there are more nav properties 
    var _query = context.Owner 
     .Include(a => a.DogList) 
     .Include(a => a.CatList); 
    ... 
    ...// rest of your code 
} 

ひとたびnavプロパティを組み込むと、それは基本的に他のテーブルに加わることになります。クエリによって生成されたSQLを見てください。お役に立てれば!

+0

ありがとうございました。非常に参考になりました。 – user972255