【发布时间】:2015-11-27 13:40:54
【问题描述】:
在 WebApi 项目的存储库类中有 GetSingleIncluding() 方法,它返回一个实体,其中包含作为参数传递的包含对象。
private readonly EFDbContext _context;
private IDbSet<T> _entities;
private IDbSet<T> Entities
{
get
{
_entities = _entities ?? _context.Set<T>();
return _entities;
}
}
public T GetSingleIncluding(int id, params Expression<Func<T, object>>[] includeProperties)
{
var query = Entities.Where(x => x.ID == id);
foreach (var includeProperty in includeProperties)
{
query = query.Include(includeProperty);
}
return query.First();
}
我在控制器中有一个动作
public HttpResponseMessage GetFull(int id, string entities)
我把它用作:
var entitiy = Repository.GetSingleIncluding(id, x => x.Person);
这里我明确传递了一个参数x => x.Persons
有没有办法通过url请求传递这个参数?例如,我会将所有对象(可以包含在当前实体中)作为字符串传递给 url
http://localhost/api/House/1/Person,Address,...
控制器会将这些参数传递给GetSingleIncluding() 方法:
Repository.GetSingleIncluding(id, x => x.Person, y => y.Address);
房屋实体
public class House : BaseEntity
{
public int PersonID { get; set; }
public int HouseID { get; set; }
public int AddressID { get; set; }
...
public virtual Person Person { get; set; }
public virtual Address Address { get; set; }
}
【问题讨论】:
-
首先,我不相信这是可能的。您可以将原始值放入查询字符串中,但不能放入
Expression。您可能可以使用反射做一些疯狂的事情,但我必须质疑 Web 浏览器规定应该包含哪些 EF 导航属性的用例。你的 UI 应该不知道你的数据层。 -
我不想将表达式放在查询字符串中。它应该只是一个字符串
标签: c# asp.net-mvc entity-framework asp.net-web-api