为XXX的最佳重载的方法匹配具有一些无效参数 [英] The best overloaded method match for XXX has some invalid arguments

查看:531
本文介绍了为XXX的最佳重载的方法匹配具有一些无效参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个新的初学者到实体框架。

我尝试写TE下面的方法,但我得到一个编译时错误。

 保护静态无效EntitySQLQuery(AWEntities上下文)
        {
            字符串CMD = @选择值c从AWEntities.Person为C WHERE c.FirstName = @firstName;            的ObjectQuery<&人GT;人=新的ObjectQuery<&人GT;(CMD,背景); //错误
        }



  

对于最佳重载方法匹配
  System.Data.Objects.ObjectQuery.ObjectQuery(字符串,
  System.Data.Objects.ObjectContext)'有一些无效参数



解决方案

这是实体框架的更混乱点之一。你必须认识到什么是ObjectContext的是这样做的EntityFramework的老办法,2010年大约这是的DbContext(和code在前)之前。你仍然可以得到的ObjectContext的用铸造你的DbContext到IOjbectContextAdapter,像这样的:

 ((IObjectContextAdapter)上下文).ObjectContext;

这将使您的查询是这样的:

 的ObjectQuery<&人GT;人=新的ObjectQuery<&人GT;(CMD,((IObjectContextAdapter)上下文).ObjectContext);

我不知道,如果实体结构化查询被提升了。我会考虑使用LINQ在可能的情况。

  VAR名字=弗雷德
从人AWEntities.Person返回
       其中,person.FirstName =名字
       选择人;

如果你正在读朱莉·勒曼的书,我会强烈建议你拿起所有三种。第一个是一个巨大的坟墓解释所有的基本面(并围绕做事的方式ObjectContext的书面),后两个是比较实用今天的code-第一/世界的DbContext。

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

解决方案

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). You can still get to the ObjectContext by casting your DbContext to IOjbectContextAdapter, like this:

((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.

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. 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.

这篇关于为XXX的最佳重载的方法匹配具有一些无效参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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