entity framework中的“数据读取器具有多个字段”错误

我正在使用Entity Framework执行这个简单的查询

db.Database.SqlQuery("SELECT * FROM hospital"); 

但是我收到了这个错误:

数据阅读器有多个字段。 多个字段对EDM原语或枚举类型无效。

可能是什么问题呢?

查看医院表格的样子会很有用,但假设医院由HospitalId和HospitalName组成,那么您有几个选择。

 db.Database.SqlQuery>("SELECT hospitalName FROM hospital"); //would work if all you're trying to do is get the Name db.Database.SqlQuery("SELECT * FROM hospital"); //where you define MyEntity as the same structure as the table would work db.Database.SqlQuery>>("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. Ie if field 1 is an int and field 2 is a string then Tuple 

基本上错误是代码不知道怎么把医院的结构塞进一个字符串

这解决了我的问题,一些结果没有得到应有的方式

 string storedProcedure = "Admin_AutoGenerateKeywordsFortblCompany @Company_ID=" + CompId; var s= db.ExecuteStoreQuery>("exec " + storedProcedure).ToList(); 

这里可以捕获单个或多个结果

如果您尝试执行ab INSERT,UPATE或DELETE命令,也可能会出现此错误

而不是使用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"); } 

有关详细信息,请访问 – http://www.entityframeworktutorial.net/EntityFramework4.3/raw-sql-query-in-entity-framework.aspx

根据cgotberg给我的狡猾答案,我将回答我自己的问题。 该代码的问题是某些表的字段与数据库中的字段不同(我正在寻找exception,但我找不到它,对不起),但实际上存在。 出于某种原因,我不得不将表的所有字段添加到查询字符串中。 我的意思是,我不得不写“SELECT hospital_phone,holpital_street等医院”,如果我写“SELECT hospital_name FROM hospital”,就会发生exception。

希望我能解释得很清楚。