Multi-Level Includes in CodeFirst - EntityFrameWork

三世轮回 提交于 2019-11-28 22:26:11

问题


It is working code;

IQueryable<Product> productQuery = ctx.Set<Product>().Where(p => p.Id == id).(Include"Contexts.AdditionalProperties.Field");

But you know that it could not produce compile time error if we made mistake in string statement in "Contexts.AdditionalProperties.Field"

I would like to write code below;

IQueryable<Product> productQuery = ctx.Set<Product>().Where(p => p.Id == id).Include(p => p.Contexts);

But above statement could not give chance to define AdditionalProperties and Field.

What should we do?

I would like to write as more than one include for build query.

Thanks.


回答1:


If AdditionalProperties is a single reference to another object:

using System.Data.Entity;
...
IQueryable<Product> productQuery = ctx.Set<Product>()
        .Include(p => p.Contexts.AdditionalProperties.Field)
        .Where(p => p.Id == id);


If AdditionalProperties is a collection then you can use the Select method:

IQueryable<Product> productQuery = ctx.Set<Product>()
        .Include(p => p.Contexts.AdditionalProperties.Select(a => a.Field))
        .Where(p => p.Id == id);

Don't forget to import System.Data.Entity namespace in your class file!



来源:https://stackoverflow.com/questions/4750900/multi-level-includes-in-codefirst-entityframework

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!