C# 实体框架中的“数据读取器具有多个字段”错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15932239/
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
"The data reader has more than one field" error in Entity Framework
提问by
I'm executing this simple query with Entity Framework
我正在使用实体框架执行这个简单的查询
db.Database.SqlQuery<string>("SELECT * FROM hospital");
But I got this error:
但我收到了这个错误:
The data reader has more than one field. Multiple fields are not valid for EDM primitive or enumeration types.
数据读取器有多个字段。多个字段对 EDM 原语或枚举类型无效。
What could be the problem?
可能是什么问题呢?
采纳答案by cgotberg
It would be useful to see what the hospital table looks like but assuming something simple like hospital consists of HospitalId and HospitalName then you have a couple of choices.
查看医院表的外观会很有用,但假设像医院这样简单的东西由 HospitalId 和 HospitalName 组成,那么您有几个选择。
db.Database.SqlQuery<IEnumerable<string>>("SELECT hospitalName FROM hospital"); //would work if all you're trying to do is get the Name
db.Database.SqlQuery<MyEntity>("SELECT * FROM hospital"); //where you define MyEntity as the same structure as the table would work
db.Database.SqlQuery<IEnumerable<Tuple<int, string>>>("SELECT * FROM hospital"); // would theoretically work although I haven't tried it. Where the Tuple items would have to match the database types in order. I.e. if field 1 is an int and field 2 is a string then Tuple<int,string>
Basically the error is the code doesn't know how to stuff the structure of hospital into a string
基本上错误是代码不知道如何将医院的结构填充到字符串中
回答by cgotberg
Based on the secound answer that cgotberggave me, Im going to answer my own question. The problem with that code was that some table's field was not the same as it is in the database(I was looking for the exception but I could not found it, sorry for that) but it actually exist. For some reason i had to add all the fields of the tables to the query string. I mean, i had to write "SELECT hospital_phone, holpital_street, etc, etcFROM hospital", if i write "SELECT hospital_nameFROM hospital" the exception occurs.
根据cgotberg给我的第二个答案,我要回答我自己的问题。该代码的问题在于某些表的字段与数据库中的字段不同(我正在寻找异常但我找不到它,抱歉)但它确实存在。出于某种原因,我不得不将表的所有字段添加到查询字符串中。我的意思是,我不得不写“SELECT hospital_phone,holpital_street,等,等出院”,如果我写“SELECT hospital_name从医院”出现异常。
Hope I've it explained well.
希望我已经解释得很好。
回答by cgotberg
This solved my issue, some results where not getting the way it was supposed to
这解决了我的问题,有些结果没有达到预期的效果
string storedProcedure = "Admin_AutoGenerateKeywordsFortblCompany @Company_ID=" + CompId;
var s= db.ExecuteStoreQuery<List<string>>("exec " + storedProcedure).ToList();
here single or multiple results can be caught
这里可以捕获单个或多个结果
回答by Mauricio Gracia Gutierrez
You might also get this error if you are trying to execute an INSERT
, UPATE
or DELETE
command
如果您尝试执行INSERT
,UPATE
或DELETE
命令,您也可能会收到此错误
Instead of using SqlQuery
use ExecuteSqlCommand
而不是使用SqlQuery
使用ExecuteSqlCommand
using (var ctx = new SchoolDBEntities())
{
int noOfRowUpdated = ctx.Database.ExecuteSqlCommand("Update student
set studentname ='changed student by command' where studentid=1");
int noOfRowInserted = ctx.Database.ExecuteSqlCommand("insert into student(studentname)
values('New Student')");
int noOfRowDeleted = ctx.Database.ExecuteSqlCommand("delete from student
where studentid=1");
}
For more details visit - http://www.entityframeworktutorial.net/EntityFramework4.3/raw-sql-query-in-entity-framework.aspx
有关更多详细信息,请访问 - http://www.entityframeworktutorial.net/EntityFramework4.3/raw-sql-query-in-entity-framework.aspx