数据读取器与EF 6上指定的不兼容

时间:2015-07-07 15:22:19

标签: sql-server entity-framework

我试图看看是否有人知道我为什么会收到此错误:

  

数据阅读器与指定的内容不兼容   ' model.getCoursesForCalendar_Result&#39 ;.该类型的成员,   ' edutaskid',在数据阅读器中没有相应的列   同名。

我有一个存储过程,我添加到EF 6,它有和if语句返回不同的数据,但在If分支的两端返回相同数量的列,我是一直在谷歌搜索,似乎有点同意也许返回的列不一样但在我的情况下它不适用,我也尝试更改我用作Id的列不可为空,因为EF自动将它设置为Nullable,但仍然没有运气,奇怪的部分是我评论了几乎所有的代码并让我的存储过程如此简单:

 SELECT 1 AS edutaskid, 
                 '1' AS title, 
                 't' AS fromdate, 
                 't' AS thrudate 
                 FROM edutaskoffer

仍然得到错误,但如果我这样离开:

SELECT 1 AS edutaskid, 
                     '1' AS title, 
                     't' AS fromdate, 
                     't' AS thrudate 

我没有得到错误,唯一的区别是我不再查询表,现在只返回一行,所以我认为可能与行有关,所以我创建了一个temp具有相同列并插入两行并选择这些行的表,我确实得到了两行:

DECLARE @CalendarCoursesTable TABLE
      (
         edutaskId INT NOT NULL,
         title     VARCHAR(150),
         fromdate  VARCHAR(150), 
         thrudate  VARCHAR(150)
      );

INSERT INTO @CalendarCoursesTable
          (edutaskId,
                 title,
                 fromdate,
                 thrudate)
          VALUES (1,'1','t','t');

          INSERT INTO @CalendarCoursesTable
          (edutaskId,
                 title,
                 fromdate,
                 thrudate)
          VALUES (2,'2','U','U');

          SELECT edutaskId,
                 title,
                 fromdate,
                 thrudate
          FROM @CalendarCoursesTable;

在前面的示例中,我没有得到错误,唯一的问题是当我使用我实际在数据库上创建的表时。我还按照此链接提到的内容:The data reader is incompatible with the specified class,但我的列映射是正确的:image

任何人都知道可能出现什么问题?

2 个答案:

答案 0 :(得分:0)

你尝试过类似的事情吗?

SELECT cast(1 as int) AS edutaskid, 
             '1' AS title, 
             't' AS fromdate, 
             't' AS thrudate 
             FROM edutaskoffer

答案 1 :(得分:0)

I found the problem, the user that I was using in the database, didn't have permissions to read those tables from another database, the error got hidden in the stored procedure and didn't come back to the EF so at the end in the EF it didn't find any columns to map and caused that error.