当我尝试使用“Include(”PropertyName“)”“时,为什么会得到”Argument“异常在查询EF? [英] Why do I get Argument exception when I try to use "Include("PropertyName")" in query EF?

查看:97
本文介绍了当我尝试使用“Include(”PropertyName“)”“时,为什么会得到”Argument“异常在查询EF?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码

var query = BAL.ApplicationInfo.db.Items.Include("Units").ToList();

但它给我这个错误


值不能为空。参数名称:来源

Value cannot be null. Parameter name: source

我的项目实体有一个名为Units的属性,实际上在项之间有关系单位实体。但是我想加载父记录的子记录,但是当我尝试使用上面的代码查询时,我收到上述错误。

My Item entity has a property called "Units" , actually there is relation between Items and Units entities. However I want to load child records with parent records, but I am getting the above error whenever I try to query with above code.

有人可以帮助我完成这项工作吗?

Can someone help me to get this done ?

我的上下文是一个DBcontext而不是ObjectContext

My Context is a DBcontext not a ObjectContext

推荐答案

问题。其实问题是我没有一个使用指令到$ code> System.Data.Entity 命名空间在我的顶部类。

I solved the problem. Actually the problem was I didn't have a using directive to System.Data.Entity namespace at the top of my class.

甚至以为我可以在我的查询中使用Include(PropertyName)名称,我无法使用Include(x => x.Childs)。但是使用System.Data.Entity添加在我的课堂之上,我可以使用Include在这两种方式查询,我的错误解决了。

Even thought I could use "Include("PropertyName)" name in my query, I couldn't use "Include(x=> x.Childs)". But after adding "using System.Data.Entity" on top of my class I can use "Include" in these both ways on a query and my error is solved.

这篇关于当我尝试使用“Include(”PropertyName“)”“时,为什么会得到”Argument“异常在查询EF?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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