数据读取器与指定的类不兼容 [英] The data reader is incompatible with the specified class

查看:301
本文介绍了数据读取器与指定的类不兼容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以有一些其他类似的类型的帖子,但没有匹配我正在尝试的简单任务。
以下列方式通过 EF6代码运行存储过程

so there are a few other similar type posts about, but none matching the simple task I am attempting. just running a stored proc through EF6 code first in the following manner

var results = context.DataBase.SqlQuery<MyClass>("exec spGetStuff @param1, @param2", new SqlParameter[] {new SqlParameter("param1",value), new SqlParameter("param2", value2)});

我已经在很多场合使用这种方法没有问题。
我映射结果的类是很好的许多属性,但所有需要它的都标有 [Column(dbfieldname)] 属性

I have used this method on many occasions with no issue. the class I am mapping the results to is pretty nasty with many properties, but all that need it are marked with the [Column("dbfieldname")] attribute.

所有存储过程正在通过使用

all the stored proc is doing is returning some results by using a

SELECT * FROM(
SELECT 
    ROW_NUMBER() OVER ( PARTITION BY X,Y,Z ORDER BY A) [RowNumber]
,*
FROM
MyTableNAme
WHERE
...) S
WHERE s.RowNumber = 1

不插入,更新或任何像这样的幻想。

not inserting, updating or anything fantastical like that.


数据读取器与指定的MyClass不兼容。类型PropertyNameName的成员在数据读取器中没有相同的列。

The data reader is incompatible with the specified 'MyClass'. A member of the type, 'PropertyNameName', does not have a corresponding column in the data reader with the same name.

如果我将类属性更改为db列名称,似乎工作正常:我可以更改前几个属性,然后在类中的其他属性将失败...但是如果可以的话我不太想这样做避免它,因为DB中的大多数列命名非常严重,所以我猜我的问题是为什么忽略 [Column()] 属性从来没有让我失望过。
或是row_number的问题,我已经尝试向类中添加/或从查询结果集中删除。

if I do change the class properties to the db column names it seems to work fine:I can change the first few properties and it will then fail on other ones in the class... however I do not really want to do that if I can possibly avoid it as most of the columns in the DB are named very badly indeed, so I guess my question is why is it ignoring the [Column()] attributes that have never failed me before. or is the issue the row_number, which I have tried adding to the class an/or removing from the query resultsset.

推荐答案

我们也看到这个问题,从升级和重构EF6从5.存储过程返回,我们有一个复杂类型定义在我们的edmx。似乎所有的匹配,但我们得到相同类型的错误,当这样调用

We're seeing this issue too since upgrading and refactoring for EF6 from 5. Stored procedure returns and we have a Complex Type defined in our edmx. It seems that everything matches up but we get the same type of error when calling like this

return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction<GetColumnValueSet_Result>("GetColumnValueSet", param1, param2, param3);

所以在尝试了几件事情之后,这是我发现的。我打开了我的edmx,并转到Visual Studio中的Model Browser。在功能导入下,我找到了我的sproc。右键单击并选择功能导入映射。事实证明,即使复杂的类型是正确的,它似乎一切都应该匹配功能导入映射正在考虑到sproc返回一个名为CodeValue的列而不是Value(这是它实际导入的内容)。

So after trying a few things this is what I found. I opened my edmx and went to the Model Browser within Visual Studio. Under Function Imports I found my sproc. Right clicked and chose Function Import Mapping. It turned out that even though the complex type was correct and it appeared everything should have matched up the Function Import Mapping was thinking the sproc was returning a column called CodeValue instead of Value (which is what it was actually importing).

所以由于某些原因,当sproc列得到时,它会出现错误的映射名称。

So for some reason when the sproc columns were gotten it go a wrong name for the mapping.

这篇关于数据读取器与指定的类不兼容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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