asp.net-mvc System.Data.Entity.dll 中的异常但未在用户代码中处理

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17383405/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 02:58:53  来源:igfitidea点击:

Exception in System.Data.Entity.dll but was not handled in user code

asp.net-mvclinqc#-4.0

提问by Daniel Chernenkov

Hi guys,

嗨,大家好,

I need to count number of rows in my database with the entity framework. I am using the LINQ method "Count".

我需要使用实体框架计算数据库中的行数。我正在使用 LINQ 方法“计数”。

Here is the code:

这是代码:

QvDb dba = new QvDb();
if (dba.KUser.Count(us => us.FacebookId == values["FacebookId"]) == 0)

As you can see the values["FacebookId"]it's a post array variable, and the dbaobject variable it's the database model builder.

如您所见,values["FacebookId"]它是一个后数组变量,而dba对象变量则是数据库模型构建器。

When i am trying to access to the page i got this exception:

当我尝试访问该页面时,出现此异常:

An exception of type 'System.NotSupportedException' occurred in System.Data.Entity.dll but was not handled in user code

Additional information: LINQ to Entities does not recognize the method 'System.String get_Item(System.String)' method, and this method cannot be translated into a store expression.

System.Data.Entity.dll 中出现“System.NotSupportedException”类型的异常,但未在用户代码中处理

附加信息:LINQ to Entities 无法识别“System.String get_Item(System.String)”方法,并且此方法无法转换为存储表达式。

for the record - the array is not null. it's a string that posted from the form.

记录 - 数组不为空。它是从表单发布的字符串。

回答by Matt Houser

When using LINQ to entities, all parts of your LINQ statement must be supported by the database.

使用 LINQ to entity 时,数据库必须支持 LINQ 语句的所有部分。

values["FacebookId"]is a local dictionary. So it cannot be executed on the remote SQL database.

values["FacebookId"]是本地字典。所以不能在远程SQL数据库上执行。

Pull the value from the dictionary first into a local variable, then execute your LINQ statement.

首先将字典中的值提取到局部变量中,然后执行您的 LINQ 语句。

QvDb dba = new QvDb();
var id = values["FacebookId"];
if (dba.KUser.Count(us => us.FacebookId == id) == 0)