2012-06-11 87 views
0

使用存儲庫模式,我有一個類型T(使用泛型)的通用存儲庫EFRepository實體框架4.3.1不填充子實體

我有一個叫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); 

我的機器類是什麼樣子這個(有一些細節省略):

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測試版)時,未安裝已安裝的操作系統和許可證類型實體,它們似乎被實例化,但ID爲0,其他屬性爲空值。在機器實體中,直接使用正確的ID填充InstalledOSId和LicenceTypeId屬性。

如果我調試應用程序一直到AllIncluding方法,我可以看到構造的SELECT查詢包含正確的表和連接,但仍然沒有骰子。

我不確定它的任何後果,但我將IQueryable一直傳回控制器。我假設視圖渲染(返回視圖(結果))可以管理枚舉?

+1

幾件事情:1。我不知道'表達式'中的'object'被EF所讚賞。 2.模型中的導航屬性不是虛擬的。 3.嘗試在你的調用之後創建一個ToList(),並獲取錯誤(如果你有一個)。 –

+0

LicenceType和InstalledOS的PK是否也是一個int? – mfussenegger

+0

@mfussenegger,是的所有實體都是int。 –

回答

0

這裏是一個非常簡單的實現你這裏說的是什麼工作 - 或許你可以把它比作你必須找到不同的內容:

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 class InstalledOS 
{ 
    public int InstalledOSId { get; set; } 
} 

public class LicenceType 
{ 
    public int LicenceTypeId { get; set; } 
} 

public class Context : DbContext 
{ 
    public DbSet<Machine> Machines { get; set; } 
    public DbSet<LicenceType> LicenceTypes { get; set; } 
    public DbSet<InstalledOS> InstalledOSs { get; set; } 
} 

public class Repository<T> where T : class 
{ 
    private DbSet<T> _dbSet; 
    public Repository(DbSet<T> dbset) 
    { 
     _dbSet = dbset; 
    } 

    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; 
    } 
} 

class Program 
{ 
    static void Main(string[] args) 
    { 
     Database.SetInitializer(new DropCreateDatabaseAlways<Context>()); 
     Context context = new Context(); 

     InstalledOS os1 = new InstalledOS(); 
     context.InstalledOSs.Add(os1); 

     LicenceType l1 = new LicenceType(); 
     context.LicenceTypes.Add(l1); 

     Machine m1 = new Machine 
     { 
      InstalledOS = os1, 
      LicenceType = l1 
     }; 
     context.Machines.Add(m1); 

     context.SaveChanges(); 

     Repository<Machine> repo = new Repository<Machine>(context.Machines); 

     var query = repo.AllIncluding(m => m.InstalledOS, m => m.LicenceType); 
     Machine m2 = query.First(); 

     Console.WriteLine(m2.InstalledOS.InstalledOSId); 
     Console.ReadLine(); 

    } 
}