为XXX的最佳重载的方法匹配具有一些无效参数

编程入门 行业动态 更新时间:2024-10-10 09:18:37
本文介绍了为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的最佳重载的方法匹配具有一些无效参数

本文发布于:2023-06-03 07:25:36,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/471221.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:参数   方法   XXX

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!