リポジトリ パターンを使用して、EFRepository
タイプ T の汎用リポジトリを作成しました (ジェネリックを使用)。
AllIncluding
エンティティとその子エンティティを取得するために使用されるメソッドがあります。
public IQueryable<T> AllIncluding(params Expression<Func<T,
object>>[] includeProperties)
{
IQueryable<T> query = _dbSet;
foreach (var includeProperty in includeProperties)
{
query = query.Include(includeProperty);
}
return query;
}
呼び出しで次の構文を使用します。
_machineRepository.AllIncluding(machine => machine.InstalledOS,
machine => machine.LicenceType,
machine => machine.User);
私の Machine クラスは次のようになります (一部の詳細は省略されています)。
public class Machine
{
public int MachineId { get; set; }
public int InstalledOSId { get; set; }
public InstalledOS InstalledOS { get; set; }
public int LicenceTypeId { get; set; }
public LicenceType LicenceType { get; set; }
public int UserId { get; set; }
public User User { get; set; }
}
私が見つけたのは、マシン エンティティのビュー (ASP.NET MVC 4 ベータ版を使用) をレンダリングするときに、インストールされた OS とライセンスの種類のエンティティが入力されておらず、インスタンス化されているように見えますが、ID は 0 であり、他のプロパティは null です。Machine エンティティでは、InstalledOSId および LicenceTypeId プロパティに正しい ID が直接入力されます。
アプリケーションを AllInclusive メソッドまでデバッグすると、構築された SELECT クエリに正しいテーブルと結合が含まれていることがわかりますが、ダイスはありません。
結果がどうなるかはわかりませんが、コントローラーまでずっと IQueryable を渡しています。ビューのレンダリング (View(results) を返す) が列挙を管理できると仮定していますか?