I'm executing this simple query with Entity Framework
我正在使用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?
可能是什么问题呢?
4 个解决方案
#1
34
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
基本上错误是代码不知道怎么把医院的结构塞进一个字符串
#2
6
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
这里可以捕获单个或多个结果
#3
2
You might also get this error if you are trying to execute ab INSERT, UPATE or DELETE command
如果您尝试执行ab 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
#4
0
Based on the secound answer that cgotberg gave 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, etc FROM hospital", if i write "SELECT hospital_name FROM hospital" the exception occurs.
根据cgotberg给我的狡猾答案,我将回答我自己的问题。该代码的问题是某些表的字段与数据库中的字段不同(我正在寻找异常,但我找不到它,对不起),但实际上存在。出于某种原因,我不得不将表的所有字段添加到查询字符串中。我的意思是,我不得不写“SELECT hospital_phone,holpital_street等医院”,如果我写“SELECT hospital_name FROM hospital”,就会发生异常。
Hope I've it explained well.
希望我能解释得很清楚。
#1
34
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
基本上错误是代码不知道怎么把医院的结构塞进一个字符串
#2
6
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
这里可以捕获单个或多个结果
#3
2
You might also get this error if you are trying to execute ab INSERT, UPATE or DELETE command
如果您尝试执行ab 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
#4
0
Based on the secound answer that cgotberg gave 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, etc FROM hospital", if i write "SELECT hospital_name FROM hospital" the exception occurs.
根据cgotberg给我的狡猾答案,我将回答我自己的问题。该代码的问题是某些表的字段与数据库中的字段不同(我正在寻找异常,但我找不到它,对不起),但实际上存在。出于某种原因,我不得不将表的所有字段添加到查询字符串中。我的意思是,我不得不写“SELECT hospital_phone,holpital_street等医院”,如果我写“SELECT hospital_name FROM hospital”,就会发生异常。
Hope I've it explained well.
希望我能解释得很清楚。