【发布时间】:2012-07-25 18:23:28
【问题描述】:
我目前有一个完整的通用存储库,但我缺少一个功能,那就是使用
Include() 和Find() 一起使用。
所以现在我有:
public E FindById<E>(int id) where E : class
{
return DataContext.Set<E>().Find(id);
}
使用调用
var person = PersonRepo.FindById<Person>(personId);
我想要类似的东西:
var person = PersonRepo.FindByIdWithIncludes<Person>(personId,new[]{"State.Address"});
所以,类似这样的事情(这只是一个测试):
public E FindByIdWithIncludes<E>(int id, string[] includes) where E : class
{
var entitySet = DataContext.Set<E>();
DbQuery<E> entityQuery;
foreach (var include in includes)
{
entityQuery = entitySet.Include(include);
}
return entityQuery.Find(id); //this is were it breaks
}
有可能吗?
【问题讨论】:
标签: c# entity-framework generics