查询具有最低级别过滤器的多级实体
query multi-level entity with filter at the lowest level
所以我有 3 个实体 类:
public partial class Event
{
public Event()
{
Recurrences = new HashSet<Recurrence>();
}
public int Id { get; set; }
public ICollection<Recurrence> Recurrences { get; set; }
}
public partial class Recurrence
{
public Recurrence()
{
AspNetUsers = new HashSet<AspNetUser>();
}
public int Id { get; set; }
public int EventId { get; set; }
public ICollection<AspNetUser> AspNetUsers { get; set; }
}
public partial class AspNetUser
{
public AspNetUser()
{
Recurrences = new HashSet<Recurrence>();
}
public string Id { get; set; }
public string UserName { get; set; }
public ICollection<Recurrence> Recurrences { get; set; }
}
我想在给定 aspnetuser.id 的情况下使用行到实体来获取事件。到目前为止,这是我所拥有的,但它返回了一个错误:
// GET: api/Events?userId={userId}
public IQueryable<Event> GetEvents(string userId)
{
return db.Events
.Include(e => e.Recurrences
.Select(u => u.AspNetUsers.Where(i => i.Id == userId)));
}
当我排除 where 子句时它工作正常。请帮忙。
提前致谢!
我认为 Include() 的含义与您认为的不同。 (https://msdn.microsoft.com/en-us/library/bb738708%28v=vs.110%29.aspx) What it does is tell the db set to be sure to bring in relationships for that object. By default (last I checked), the db context will auto pull in all relationships, so this isn't necessary. However, if you've turned off the lazy-loading (http://www.entityframeworktutorial.net/EntityFramework4.3/lazy-loading-with-dbcontext.aspx) 然后你需要 .Include() 所有你想在查询中拥有的关系。
这应该可以解决您的问题。不过,我不保证生成的 SQL 不会很傻。
如果您打开了延迟加载:
db.Events.Include("Recurrences").Include("Recurrences.AspNetUsers")
.Where(e => e.Recurrences
.Any(r => r.AspNetUsers
.Any(u => u.Id ==userId)));
如果您关闭了延迟加载:
db.Events
.Where(e => e.Recurrences
.Any(r => r.AspNetUsers
.Any(u => u.Id ==userId)));
此外,如果您无法看到错误,您可以在返回之前对查询进行 .ToList() 处理,这样它就会在您的代码中失败,而不是深入到 Web API 堆栈中。就个人而言,我喜欢这样做,这样我就可以 try/catch 查询并正确处理它。
所以我有 3 个实体 类:
public partial class Event
{
public Event()
{
Recurrences = new HashSet<Recurrence>();
}
public int Id { get; set; }
public ICollection<Recurrence> Recurrences { get; set; }
}
public partial class Recurrence
{
public Recurrence()
{
AspNetUsers = new HashSet<AspNetUser>();
}
public int Id { get; set; }
public int EventId { get; set; }
public ICollection<AspNetUser> AspNetUsers { get; set; }
}
public partial class AspNetUser
{
public AspNetUser()
{
Recurrences = new HashSet<Recurrence>();
}
public string Id { get; set; }
public string UserName { get; set; }
public ICollection<Recurrence> Recurrences { get; set; }
}
我想在给定 aspnetuser.id 的情况下使用行到实体来获取事件。到目前为止,这是我所拥有的,但它返回了一个错误:
// GET: api/Events?userId={userId}
public IQueryable<Event> GetEvents(string userId)
{
return db.Events
.Include(e => e.Recurrences
.Select(u => u.AspNetUsers.Where(i => i.Id == userId)));
}
当我排除 where 子句时它工作正常。请帮忙。 提前致谢!
我认为 Include() 的含义与您认为的不同。 (https://msdn.microsoft.com/en-us/library/bb738708%28v=vs.110%29.aspx) What it does is tell the db set to be sure to bring in relationships for that object. By default (last I checked), the db context will auto pull in all relationships, so this isn't necessary. However, if you've turned off the lazy-loading (http://www.entityframeworktutorial.net/EntityFramework4.3/lazy-loading-with-dbcontext.aspx) 然后你需要 .Include() 所有你想在查询中拥有的关系。
这应该可以解决您的问题。不过,我不保证生成的 SQL 不会很傻。
如果您打开了延迟加载:
db.Events.Include("Recurrences").Include("Recurrences.AspNetUsers")
.Where(e => e.Recurrences
.Any(r => r.AspNetUsers
.Any(u => u.Id ==userId)));
如果您关闭了延迟加载:
db.Events
.Where(e => e.Recurrences
.Any(r => r.AspNetUsers
.Any(u => u.Id ==userId)));
此外,如果您无法看到错误,您可以在返回之前对查询进行 .ToList() 处理,这样它就会在您的代码中失败,而不是深入到 Web API 堆栈中。就个人而言,我喜欢这样做,这样我就可以 try/catch 查询并正确处理它。