c# - Entity Framework 中的 "The data reader has more than one field"错误

标签 c# .net entity-framework

我正在使用 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/

相关文章:

c# - 使用 StreamWriter 后如何重新启动它?

c# - 如果缺少字符串,如何在字符串的开头和结尾加上<p>?

c# - MonoDevelop 2.4.1 未在 Windows 7 上运行最终无法加载 glibsharpglue-2

c# - 删除最后 N 条记录, Entity Framework

c# - MVVM 在选择更改时调用异步方法

.net - Twitter API - 获取 GPS 点附近的推文

c# - 检查文件/目录是否存在 : is there a better way?

c# - 列表数据结构C#效率

entity-framework - 将 DbContext 注入(inject) Repository 类库

c# - DbContext 加载缓慢