使用存储库模式,我有一个类型为T的通用存储库EFRepository
(使用泛型)。
我有一个名为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);
My 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; }
}
我发现的是,当我为Machine实体渲染View(使用ASP.NET MVC 4 beta)时,未填充Installed OS和License Type实体,它们似乎是实例化的但是ID为0,其他属性为null。在Machine实体中,InstalledOSId和LicenceTypeId属性使用正确的ID填充。
如果我将应用程序一直调试到AllIncluding方法,我可以看到构造的SELECT查询包含正确的表和连接,但仍然没有骰子。
我不确定它是否有任何后果,但我将IQueryable一直传回控制器。我假设视图呈现(返回视图(结果))可以管理枚举?
答案 0 :(得分: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();
}
}