wpf 数据读取器不兼容...成员在数据读取器中没有对应的列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1494273/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 21:03:22  来源:igfitidea点击:

Data reader is incompatible... member does not have corresponding column in data reader

wpfvisual-studio-2008sql-server-2008modelentity

提问by erinql

Using VS 2008, SQL Server 2008 and WPF 3.5, I've made some changes to my schema and updated the model. It compiles and runs fine, until the client app calls for a specific entity and I get the following (actual names replaced):

使用 VS 2008、SQL Server 2008 和 WPF 3.5,我对架构进行了一些更改并更新了模型。它编译并运行良好,直到客户端应用程序调用特定实体并且我得到以下信息(实际名称已替换):

The data reader is incompatible with the specified '<Model>.<ViewBasedEntity>'. A member of the type, '<Property>', does not have a corresponding column in the data reader with the same name.

I've searched through the services app for related entity and property names, tried renaming properties in the Table Map, Seems to be a number of others out there reporting the same error, but can't seem to find a timely answer....

我在服务应用程序中搜索了相关的实体和属性名称,尝试重命名表映射中的属性,似乎有很多其他人报告了同样的错误,但似乎无法及时找到答案...... .

...Does anyone know how to track this down, and if so, is there a fix or methodology to follow to avoid in the future?

...有谁知道如何追踪这种情况,如果是这样,是否有解决方法或方法可以避免将来避免?

回答by Calvin Fisher

Rather than updating the view, you can also update the Function Import:

除了更新视图,您还可以更新函数导入:

  • Go to the Model Browser window
  • Expand the EntityContainer
  • Open the Function Import in the Mapping Details window
  • If the entity name (left column) does not match the expected field name (right column), you can change the right-hand column to match what the returned field is actually called.
  • 转到模型浏览器窗口
  • 展开实体容器
  • 在映射详细信息窗口中打开函数导入
  • 如果实体名称(左列)与预期的字段名称(右列)不匹配,您可以更改右侧列以匹配实际调用的返回字段。

回答by JamieA

I had a similar issue which produced the same error message - the problem was that a column name returned by the proc included a space.

我有一个类似的问题,它产生了相同的错误消息 - 问题是 proc 返回的列名包含一个空格。

When creating the complex type, [my column]was created as my_column.

创建复杂类型时,[my column]创建为my_column.

Then when executing the proc with ExecuteStoreQuery, my_columndid not exist in the data reader, as the proc still returned [my column].

然后当用 执行 proc 时ExecuteStoreQuerymy_column数据读取器中不存在,因为 proc 仍然返回[my column]

Solution: remove the space from proc column name and recreate your complex type for the imported function.

解决方案:从 proc 列名中删除空格并为导入的函数重新创建复杂类型。

回答by erinql

Ok - here's the skinny:

好的 - 这是瘦子:

That particular view was setup as a Return Type for a Stored Procedure that had to be setup as a Function Import in the Model's Entity Container.

该特定视图被设置为存储过程的返回类型,该存储过程必须在模型的实体容器中设置为函数导入。

I had updated that view to fit new reporting requirements, not realizing it's significance to the Function Import. The additional fields are not part of the data set recognized for this purpose, so it couldn't find a match for any of them.

我更新了该视图以适应新的报告要求,但没有意识到它对函数导入的重要性。附加字段不是为此目的识别的数据集的一部分,因此无法找到其中任何一个的匹配项。

So I duplicated the view and suffixed it with 'Report', then reverted the original back to it's expected set of return fields.

因此,我复制了视图并为其添加了“报告”后缀,然后将原始视图恢复为预期的返回字段集。

Voila!

瞧!

回答by Bob Sheehan

Ive seen this happen on data objects that vary the result set (for example a sproc with an if statement) The error message is Entity Frame work/ Data reader telling you that it expected column x and it wasn't returned.

我已经看到这种情况发生在改变结果集的数据对象上(例如带有 if 语句的 sproc)错误消息是实体框架工作/数据读取器告诉您它期望列 x 并且没有返回。

To work around this you can either a) Make sure all paths of your sproc return the same column names b) Use Database.ExecuteSqlCommand

要解决此问题,您可以 a) 确保 sproc 的所有路径都返回相同的列名 b) 使用 Database.ExecuteSqlCommand

回答by santos

I came up against the same issue when using Sprocs recently. I had some conditonal SQL in the sproc along the lines of a couple of 'if' statements

我最近在使用 Sprocs 时遇到了同样的问题。我在 sproc 中有一些条件 SQL 沿着几个“if”语句

IF @param1 = 'knownValue'
BEGIN
SELECT * FROM EntityType WHERE ID = 'somevalue'
END
ELSE IF @param1 = 'knownValue2'
BEGIN
SELECT * FROM EntityType WHERE ID = 'somevalue'
END

here's my EF code:

这是我的 EF 代码:

return context.Database.SqlQuery<EntityType>(
            "[NAV].[GeEntityType] @Date, @ID",
            new SqlParameter("Date", paramDate),
            new SqlParameter("ID", paramId)).ToList();

I had not catered for scenarios where neither 'if' statements returned true, so the sproc wasn't even returning an empty resultset and caused EF to throw this error. Without returning even an empty reader, EF had nothing to match when mapping columns to properties.

我没有考虑到“if”语句都不返回 true 的情况,因此 sproc 甚至没有返回空结果集并导致 EF 抛出此错误。即使没有返回空的读取器,EF 在将列映射到属性时也没有任何匹配项。

Hope it helps.

希望能帮助到你。

回答by Leon

Easy Way:

简单的方法:

  1. Once SP is complete update EF Model so it is available.
  2. Add PRINT statement to SP, re-compile and run.
  3. Comment out Dynamic SQL and put in PRINT statement, recompile ensure it runs.
  4. Now do Function Import in EF, columns will show.
  5. Change SP back to Dynamic code and all good :)
  1. 一旦 SP 完成更新 EF 模型,使其可用。
  2. 在SP中添加PRINT语句,重新编译运行。
  3. 注释掉动态 SQL 并放入 PRINT 语句,重新编译确保它运行。
  4. 现在在 EF 中执行函数导入,列将显示。
  5. 将 SP 改回动态代码,一切都很好:)