“数据读取器具有多个字段”。实体框架中的错误 [英] "The data reader has more than one field" error in Entity Framework

查看:226
本文介绍了“数据读取器具有多个字段”。实体框架中的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Entity Framework执行此简单查询

I'm executing this simple query with Entity Framework

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

但是我遇到了这个错误:

But I got this error:


数据读取器具有多个字段。多个字段对于EDM基本类型或枚举类型无效。

The data reader has more than one field. Multiple fields are not valid for EDM primitive or enumeration types.

可能是什么问题?

推荐答案

查看医院表的外观会很有用,但是假设像医院这样的简单内容由HospitalId和HospitalName组成,那么您可以有两种选择。

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.

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

这篇关于“数据读取器具有多个字段”。实体框架中的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆