CodeFirst中的多级包含-EntityFrameWork [英] Multi-Level Includes in CodeFirst - EntityFrameWork

查看:88
本文介绍了CodeFirst中的多级包含-EntityFrameWork的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是工作代码;

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

但是您知道,如果我们在"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"

我想在下面写代码;

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.

我们该怎么办?

我想为构建查询编写一个以上的include.

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

谢谢.

推荐答案

如果 AdditionalProperties 是对另一个对象的单个引用:

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);


如果 AdditionalProperties 是一个集合,则可以使用 Select 方法:


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);

别忘了在类文件中导入 System.Data.Entity 命名空间!

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

这篇关于CodeFirst中的多级包含-EntityFrameWork的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆