Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 无法使用ObjectContext.CreateQuery(实体框架和存储库)加载相关实体(急切加载)_C#_Asp.net Mvc_Entity Framework_Linq To Entities_Eager Loading - Fatal编程技术网

C# 无法使用ObjectContext.CreateQuery(实体框架和存储库)加载相关实体(急切加载)

C# 无法使用ObjectContext.CreateQuery(实体框架和存储库)加载相关实体(急切加载),c#,asp.net-mvc,entity-framework,linq-to-entities,eager-loading,C#,Asp.net Mvc,Entity Framework,Linq To Entities,Eager Loading,这里有很多我试过的东西。。。希望你能从中推断出我在做什么,我做错了什么。好的,所以我在使用此DoQuery时加载相关实体时遇到问题: public ObjectQuery<E> DoQuery(ISpecification<E> where) { return (ObjectQuery<E>)_ctx.CreateQuery<E>("[" + typeof(E).Name + "]").Where(where.EvalP

这里有很多我试过的东西。。。希望你能从中推断出我在做什么,我做错了什么。好的,所以我在使用此DoQuery时加载相关实体时遇到问题:

   public ObjectQuery<E> DoQuery(ISpecification<E> where)
   {
        return (ObjectQuery<E>)_ctx.CreateQuery<E>("[" + typeof(E).Name + "]").Where(where.EvalPredicate);
   }
但那没用

如果我的方向不对,请告诉我。有人知道在使用ObjectContext.CreateQuery时如何加载相关实体吗?任何建议或见解都有帮助

谢谢,

马特

正如克雷格在评论中指出的,这个答案的前一版本是不正确的。既然我不能删除这个答案(因为评论?),我至少会尝试留下一些不正确的东西

假设您有一个带有两个实体Product和ProductDetail的模型(.edmx)。进一步假设您想要检索给定的产品及其相关详细信息

我将确保以下工作(假设您有一个ID为1且有详细记录的产品):

var result=ctx.CreateQuery(“产品”)。包括(“产品详细信息”)。其中(p=>p.ProductId==1);
在上面的Include语句中,“ProductDetails”是产品的一个属性。 如果可行,我将尝试以下方法:

public ObjectQuery DoQuery<E>(Expression<Func<E, bool>> filter)
{
    string entitySetName = GetEntitySetName(typeof(T)); 
    return (ObjectQuery<E>)_ctx.CreateQuery<E>(entitySetName).Include("ProductDetails").Where(filter);
}
public ObjectQuery DoQuery(表达式过滤器)
{
字符串entitySetName=GetEntitySetName(typeof(T));
返回(ObjectQuery)\ u ctx.CreateQuery(entitySetName)。包括(“ProductDetails”)。其中(过滤器);
}
请注意,GetEntitySetName()不是内置函数

var result = DoQuery<Product>(p => p.ProductId == 1);
var result=DoQuery(p=>p.ProductId==1);

正如克雷格在评论中指出的,本答案的前一版本是不正确的。既然我不能删除这个答案(因为评论?),我至少会尝试留下一些不正确的东西

假设您有一个带有两个实体Product和ProductDetail的模型(.edmx)。进一步假设您想要检索给定的产品及其相关详细信息

我将确保以下工作(假设您有一个ID为1且有详细记录的产品):

var result=ctx.CreateQuery(“产品”)。包括(“产品详细信息”)。其中(p=>p.ProductId==1);
在上面的Include语句中,“ProductDetails”是产品的一个属性。 如果可行,我将尝试以下方法:

public ObjectQuery DoQuery<E>(Expression<Func<E, bool>> filter)
{
    string entitySetName = GetEntitySetName(typeof(T)); 
    return (ObjectQuery<E>)_ctx.CreateQuery<E>(entitySetName).Include("ProductDetails").Where(filter);
}
public ObjectQuery DoQuery(表达式过滤器)
{
字符串entitySetName=GetEntitySetName(typeof(T));
返回(ObjectQuery)\ u ctx.CreateQuery(entitySetName)。包括(“ProductDetails”)。其中(过滤器);
}
请注意,GetEntitySetName()不是内置函数

var result = DoQuery<Product>(p => p.ProductId == 1);
var result=DoQuery(p=>p.ProductId==1);

CreateQuery采用ESQL语句。所以你可以写这样的东西:

public ObjectQuery<E> DoQuery(ISpecification<E> where)
{
    var esql = String.Concat(
         "SELECT VALUE e1 FROM OFTYPE(", 
         GetEntitySetName(typeof(E)),
         ", ", 
         typeof(T).FullName, 
         ") AS e1");
    return Context.CreateQuery<T>(esql).Include("User").Where(where.EvalPredicate);
}
public ObjectQuery DoQuery(指定位置)
{
var esql=String.Concat(
“从类型中选择值e1(”,
GetEntitySetName(类型(E)),
", ", 
typeof(T).全名,
)作为“e1”);
返回Context.CreateQuery(esql).Include(“用户”).Where(Where.EvalPredicate);
}
…其中GetEntitySetName是您编写的一个方法,它返回实体类型的字符串实体集名称,或使用

为什么要打字?如果在模型中有继承,此ESQL将返回
ObjectQuery
,而不是没有继承的
ObjectQuery


最后,
Include
仅在实体E具有属性命名用户时有效。类型和实体集名称与包含ESQL语句无关。所以你可以写这样的东西:

public ObjectQuery<E> DoQuery(ISpecification<E> where)
{
    var esql = String.Concat(
         "SELECT VALUE e1 FROM OFTYPE(", 
         GetEntitySetName(typeof(E)),
         ", ", 
         typeof(T).FullName, 
         ") AS e1");
    return Context.CreateQuery<T>(esql).Include("User").Where(where.EvalPredicate);
}
public ObjectQuery DoQuery(指定位置)
{
var esql=String.Concat(
“从类型中选择值e1(”,
GetEntitySetName(类型(E)),
", ", 
typeof(T).全名,
)作为“e1”);
返回Context.CreateQuery(esql).Include(“用户”).Where(Where.EvalPredicate);
}
…其中GetEntitySetName是您编写的一个方法,它返回实体类型的字符串实体集名称,或使用

为什么要打字?如果在模型中有继承,此ESQL将返回
ObjectQuery
,而不是没有继承的
ObjectQuery


最后,
Include
仅在实体E具有属性命名用户时有效。类型和实体集名称与
Include

无关,我仍然没有得到返回的用户对象。。。我已经在这些行上加了分隔符,并检查是否在其中输入了正确的EntitySetName。如果您在调试可视化工具中展开查询的“结果视图”,则您有一个Users属性。当你扩展它时,你会得到用户数吗?这个答案是错误的。Include采用属性名称,而不是实体名称或实体集名称。CreateQuery,OTOH,采用ESQL,它可以包括实体集名称,但实际上不是实体集名称本身。我仍然没有得到返回的用户对象。。。我已经在这些行上加了分隔符,并检查是否在其中输入了正确的EntitySetName。如果您在调试可视化工具中展开查询的“结果视图”,则您有一个Users属性。当你扩展它时,你会得到用户数吗?这个答案是错误的。Include采用属性名称,而不是实体名称或实体集名称。CreateQuery,OTOH,采用ESQL,它可以包括实体集名称,但实际上本身不是实体集名称。