条件包含不适用于Entityt Framework 5

编程入门 行业动态 更新时间:2024-10-13 14:23:44
本文介绍了条件包含不适用于Entityt Framework 5的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

I'm trying to use the Conditional Include (explained here, but it's not retrieving the child information. Why? I think I've followed all the steps... I'm using WebApi controllers and Visual Studio 2012

我已经检查过,并且为每个房屋分配了doorType.这是多对多的关系.

I've alread checked and I have doorTypes assigned to each house. It's a many to many relationship.

我有这个:

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数组为空. 我想念什么?

The output shows all the houses correctly but the array of doorTypes is empty. What am I missing?

推荐答案

这是多对多的关系.

It's a many to many relationship.

就是这个问题.关系修正不适用于多对多关系,仅适用于一对一或一对多关系.

That's the problem. Relationship Fixup does not work for many-to-many relationships, only for one-to-one or one-to-many relationships.

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

You need to build up the navigation property manually after the query is executed. But you can do it relatively simple in this case:

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

这是为什么将导航集合显式加载到上下文对于多对多关系不起作用的原因,请参见以下问题: EF 4.1加载过滤后的子集合不适用于多对多,并提供了答案,尤其是参考Zeeshan Hirani关于关系固定的解释,以更深入地了解该主题.

It is the same reason why explicit loading of navigation collections into the context does not work for many-to-many relationships, see this question: EF 4.1 loading filtered child collections not working for many-to-many and the answer to it, especially the reference to Zeeshan Hirani's explanation about relationship fixup for a deeper background about the subject.

更多推荐

条件包含不适用于Entityt Framework 5

本文发布于:2023-10-26 17:41:46,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1530872.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不适用于   条件   Framework   Entityt

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!