1

我正在尝试使用条件包含(在此处解释,但它没有检索子信息。为什么?我想我已经遵循了所有步骤......我正在使用 WebApi 控制器和 Visual Studio 2012

我已经检查过了,我已经为每个房子分配了 doorTypes。这是多对多的关系。

我有这个:

DoorType 有这个属性

public virtual ICollection<House> Houses{ get; set; }

房子有这个属性

public virtual ICollection<Door> DoorTypes{ get; set; }

我正在查询这个方法

public IEnumerable<House> GetList(string latitude, string longitude, string idHousesTypeList)
{
    IEnumerable<int> intIds = null;

    if (!string.IsNullOrEmpty(idHousesTypeList))
    {
        var ids = idHousesTypeList.Split(',');
        intIds = ids.Select(int.Parse);
    }

    var location = DbGeography.FromText(string.Format("POINT ({0} {1})", latitude, longitude), 4326);
    var count = 0;
    var radius = 0.0;
    IEnumerable<House> houses = null;

    while (count < 5 && radius < 500)
    {
        radius += 2.5;
        var radiusLocal = radius;

        var dbquery =
            from house in Uow.Houses.GetAll()
            where house.Location.Distance(location) / 1000 <= radiusLocal
            orderby house.Location.Distance(location)
            select new
            {
                house,
                doorTypes= from doorType in house.DoorTypes
                                where intIds.Contains(doorType.Id)
                                select doorType
            };


        houses = dbquery
        .AsEnumerable()
        .Select(p => p.house);

        count = houses.Count();
    }

    if (houses != null && houses.Any())
    {
        return houses;
    }

    throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.NotFound));
}

我正在使用通用 EFRepository

public class EFRepository<T> : IRepository<T> where T : class
{
    public EFRepository(DbContext dbContext)
    {
        if (dbContext == null)
            throw new ArgumentNullException("dbContext");
        DbContext = dbContext;
        DbSet = DbContext.Set<T>();
    }

    protected DbContext DbContext { get; set; }

    protected DbSet<T> DbSet { get; set; }

    public virtual IQueryable<T> GetAll()
    {
        return DbSet;
    }

    public virtual IQueryable<T> GetAllIncluding(params Expression<Func<T, object>>[] includeProperties)
    {
        IQueryable<T> query = DbContext.Set<T>();
        foreach (var includeProperty in includeProperties)
        {
            query = query.Include(includeProperty);
        }

        return query;
    }

    public virtual T GetById(long id)
    {
        return DbSet.Find(id);
    }

    public virtual IQueryable<T> GetByPredicate(System.Linq.Expressions.Expression<Func<T, bool>> predicate)
    {
        IQueryable<T> query = DbContext.Set<T>().Where(predicate);
        return query;
    }

    public virtual IQueryable<T> GetByPredicateIncluding(System.Linq.Expressions.Expression<Func<T, bool>> predicate, params Expression<Func<T, object>>[] includeProperties)
    {
        IQueryable<T> query = DbContext.Set<T>().Where(predicate);
        foreach (var includeProperty in includeProperties)
        {
            query = query.Include(includeProperty);
        }

        return query;
    }

    public virtual void Upsert(T entity, Func<T, bool> insertExpression)
    {
        if (insertExpression.Invoke(entity))
        {
            Add(entity);
        }
        else
        {
            Update(entity);
        }
    }

    public virtual void Add(T entity)
    {
        DbEntityEntry dbEntityEntry = DbContext.Entry(entity);
        if (dbEntityEntry.State != EntityState.Detached)
        {
            dbEntityEntry.State = EntityState.Added;
        }
        else
        {
            DbSet.Add(entity);
        }
    }

    public virtual void Update(T entity)
    {
        DbEntityEntry dbEntityEntry = DbContext.Entry(entity);
        if (dbEntityEntry.State == EntityState.Detached)
        {
            DbSet.Attach(entity);
        }
        dbEntityEntry.State = EntityState.Modified;
    }

    public virtual void Delete(T entity)
    {
        DbEntityEntry dbEntityEntry = DbContext.Entry(entity);
        if (dbEntityEntry.State != EntityState.Deleted)
        {
            dbEntityEntry.State = EntityState.Deleted;
        }
        else
        {
            DbSet.Attach(entity);
            DbSet.Remove(entity);
        }
    }

    public virtual void Delete(int id)
    {
        var entity = GetById(id);
        if (entity == null) return; // not found; assume already deleted.
        Delete(entity);
    }
}

输出正确显示了所有房屋,但 doorTypes 数组为空。我错过了什么?

4

2 回答 2

1

这是多对多的关系。

那就是问题所在。关系修复不适用于多对多关系,仅适用于一对一或一对多关系。

执行查询后,您需要手动构建导航属性。但在这种情况下,您可以相对简单地做到这一点:

houses = dbquery
    .AsEnumerable()
    .Select(p => {
        p.house.DoorTypes = p.doorTypes;
        return p.house;
    });

这与将导航集合显式加载到上下文中不适用于多对多关系的原因相同,请参阅此问题:EF 4.1 loading filtered child collections not working for many-to-mans and the answer to it,尤其是参考 Zeeshan Hirani 对关系修复的解释,以获得有关该主题的更深入背景。

于 2013-01-11T18:06:13.033 回答
0

目前尚不清楚您是如何实现的,但是当您从上下文中获取所有房屋时,请Uow.Houses.GetAll()尝试将其包含在结果中: DoorTypes

return context.Houses.Include("DoorTypes");

并确保您idHousesTypeLis使用现有的 ID 传递。

更新:Include是 DbQuery 的成员。当您将 Set 转换为 时,它变得不可用IQueriable<T>。所以,试试这个:

public class HouseRepository : EFRepository<House>
{
    public override IQueryable<House> GetAll()
    {
        return DbContext.Set<House>().Include("DoorTypes");
    }
}
于 2013-01-11T07:05:55.340 回答