[英]The best overloaded method match for XXX has some invalid arguments
I'm a new beginner to the entity framework . 我是实体框架的新手。
I try to write te following method but i get a compile time error. 我尝试编写以下方法,但出现编译时错误。
protected static void EntitySQLQuery(AWEntities context)
{
string cmd = @"SELECT VALUE c FROM AWEntities.Person AS c WHERE c.FirstName = @FirstName";
ObjectQuery<Person> persons = new ObjectQuery<Person>(cmd, context);//Error
}
The best overloaded method match for 'System.Data.Objects.ObjectQuery.ObjectQuery(string, System.Data.Objects.ObjectContext)' has some invalid arguments 'System.Data.Objects.ObjectQuery.ObjectQuery(string,System.Data.Objects.ObjectContext)'的最佳重载方法匹配具有一些无效的参数
Your AWEntities context
object is not recongnized as an ObjectContext
. 您的AWEntities context
对象未确认为ObjectContext
。 Please check the type of your object. 请检查对象的类型。 Take a look at definition of ObjectContext and ObjectQuery's constructor 看一下ObjectContext和ObjectQuery的构造函数的定义
This is one of the more confusing points of Entity Framework. 这是实体框架比较混乱的点之一。 What you have to realize is that ObjectContext is the old way of doing EntityFramework, circa 2010. This was before DbContext (and code first). 您必须意识到,ObjectContext是执行EntityFramework的旧方法,大约在2010年。这是在DbContext之前(首先进行代码)。 You can still get to the ObjectContext by casting your DbContext to IOjbectContextAdapter, like this: 您仍然可以通过将DbContext强制转换为IOjbectContextAdapter来进入ObjectContext,如下所示:
((IObjectContextAdapter)context).ObjectContext;
Which would make your query look like this: 这会使您的查询看起来像这样:
ObjectQuery<Person> persons = new ObjectQuery<Person>(cmd, ((IObjectContextAdapter)context).ObjectContext);
I do not know if Entity Structured Queries are promoted any more. 我不知道是否进一步提升了实体结构化查询。 I would consider using LInQ where possible. 我会考虑尽可能使用LInQ。
var firstname = "Fred";
return from person in AWEntities.Person
where person.FirstName = firstname
select person;
If you are reading Julie Lerman's books, I would highly recommend that you pick up all three. 如果您正在阅读朱莉·莱尔曼(Julie Lerman)的书,我强烈建议您选读这三本书。 The first one is a huge tomb explaining all of the fundamentals (and is written around the ObjectContext way of doing things), the second two are more practical to today's code-first/dbcontext world. 第一个是一个巨大的坟墓,它解释了所有基础知识(并以ObjectContext的编写方式编写),第二个是对当今的“代码优先/ dbcontext”世界更为实用的东西。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.