我正在使用 Entity Framework 执行这个简单的查询
db.Database.SqlQuery<string>("SELECT * FROM hospital");
但是我得到了这个错误:
The data reader has more than one field. Multiple fields are not valid for EDM primitive or enumeration types.
可能是什么问题?
最佳答案
查看医院表的样子会很有用,但假设像医院这样简单的东西由 HospitalId 和 HospitalName 组成,那么您有几个选择。
//would work if all you're trying to do is get the Name:
db.Database.SqlQuery<IEnumerable<string>>("SELECT hospitalName FROM hospital");
//where you define MyEntity as the same structure as the table would work
db.Database.SqlQuery<MyEntity>("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>
db.Database.SqlQuery<IEnumerable<Tuple<int, string>>>("SELECT * FROM hospital");
基本上错误是代码不知道如何将医院的结构填充到字符串中
关于c# - Entity Framework 中的 "The data reader has more than one field"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15932239/