C# 数据读取器与指定的实体框架不兼容
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18791037/
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
The data reader is incompatible with the specified Entity Framework
提问by Filling The Stack is What I DO
I have a method that will return the bare min results from a sproc to fill a select menu. When I want the bare min results I pass bool getMin = true to the sproc, and when I want the complete record I pass bool getMin = false.
我有一种方法可以从 sproc 返回最小结果以填充选择菜单。当我想要最简单的结果时,我将 bool getMin = true 传递给 sproc,当我想要完整的记录时,我传递 bool getMin = false。
This is causing the Entity FrameWork error of "The data reader is incompatible with the specified"
这导致的实体框架错误“数据读取器与指定的不兼容”
The most relevant portion of the error
错误中最相关的部分
{"Message":"An error has occurred.","ExceptionMessage":"The data reader is incompatible with the specified 'CatalogModel.proc_GetFramingSystems_Result'. A member of the type, 'FrameType', does not have a corresponding column in the data reader with the same name.","ExceptionType":"System.Data.EntityCommandExecutionException",
{"Message":"发生错误。","ExceptionMessage":"数据读取器与指定的 'CatalogModel.proc_GetFramingSystems_Result' 不兼容。类型的成员 'FrameType' 在具有相同名称的数据读取器。","ExceptionType":"System.Data.EntityCommandExecutionException",
Obviously the error is telling me that when the data reader attempted to set the property 'FrameType' that is was not in the query results.
显然,错误告诉我,当数据读取器尝试设置不在查询结果中的属性“FrameType”时。
Now I understand the error, what I am wanting to know is that am I goning to have t split up this sql sproc into two sprocs or is there a work around for this?
现在我明白了这个错误,我想知道的是我是不是要把这个 sql sproc 拆分成两个 sproc 还是有解决办法?
My function below
我的功能如下
public static IEnumerable<IFramingSystem> GetFramingSystems(int brandID, string frameType, string glazeMethod, bool getMin)
{
using (CatalogEntities db = new CatalogEntities())
{
return db.proc_GetFramingSystems(brandID, frameType, glazeMethod, getMin).ToList<IFramingSystem>();
};
}
My TSQL below
我的 TSQL 下面
ALTER proc [Catelog].[proc_GetFramingSystems]
@BrandID INT,
@FrameType VARCHAR(26),
@GlazeMethod VARCHAR(7) ='Inside',
@getMin BIT = 0
as
BEGIN
SET NOCOUNT ON;
IF @getMin =0
BEGIN
SELECT c.ID,c.Name,c.Descr,c.FrameType,c.isSubFrame,
c.GlassThickness,c.GlassPosition,c.GlazingMethod,c.SillProfile
from Catelog.Component c
WHERE c.MyType ='Frame'
AND c.FrameType = @FrameType
AND c.GlazingMethod = @GlazeMethod
AND c.ID IN(
SELECT cp.ComponentID FROM Catelog.Part p JOIN
Catelog.ComponentPart cp ON p.ID = cp.PartID
WHERE p.BrandID = @BrandID
)
ORDER BY c.Name
END
ELSE
SELECT c.ID,c.Name,c.Descr
from Catelog.Component c
WHERE c.MyType ='Frame'
AND c.FrameType = @FrameType
AND c.GlazingMethod = @GlazeMethod
AND c.ID IN(
SELECT cp.ComponentID FROM Catelog.Part p JOIN
Catelog.ComponentPart cp ON p.ID = cp.PartID
WHERE p.BrandID = @BrandID
)
ORDER BY c.Name
SET NOCOUNT OFF;
END;
采纳答案by Wiktor Zychla
To me it seems that both branches of the IF
return different data, the first branch returns 9 columns where the second - only three. I believe the EF can't reflect the IFramingSystem
from the latter. Specifically, the column FrameType
(and 5 other columns) are obviously missing:
对我来说,似乎两个分支都IF
返回不同的数据,第一个分支返回 9 列,而第二个分支仅返回 3 列。我相信 EF 不能反映IFramingSystem
后者。具体来说,该列FrameType
(和其他 5 列)明显缺失:
...
SELECT c.ID,c.Name,c.Descr <- where are the remaining columns
from Catelog.Component c
...
回答by Avinash Goud N J
If you are inserting/deleting/updating (these are considered by EF as 'non-query'), and can be called by our code using
如果您正在插入/删除/更新(这些被 EF 视为“非查询”),并且可以由我们的代码使用
MyDbContext.Database.ExecuteSqlCommand(insert into Table (Col1,Col2) values (1,2));
But if are doing select query for a raw SQL statement, then use
但是,如果正在为原始 SQL 语句执行选择查询,则使用
MyDbContext.DbSet<Table_name>.SqlQuery(select * from table_name).ToList();
or
或者
MyDbContext.Database.SqlQuery(select * from table_name).ToList();
()
()
The SqlQuery()
function, in EF, for strange reasons, throw exception Insert/delete/update operation. (The exception thrown is "A member of the type, does not have a corresponding column in the data reader with the same name.") But it has actually performed operation if you open your Sql Management Studio and check for the entries.
该SqlQuery()
函数,在EF中,因为奇怪的原因,抛出异常Insert/delete/update操作。(抛出的异常是“该类型的成员,在数据读取器中没有对应的同名列。”)但是如果您打开Sql Management Studio并检查条目,它实际上已经执行了操作。
FYI http://www.entityframeworktutorial.net/EntityFramework4.3/raw-sql-query-in-entity-framework.aspx
仅供参考http://www.entityframeworktutorial.net/EntityFramework4.3/raw-sql-query-in-entity-framework.aspx
回答by midohioboarder
I understand this is an old post; but, I wanted to share what I learned tonight about this. What I found that the 'most relevant portion of the error message is stating' is this.
我知道这是一个旧帖子;但是,我想分享我今晚在这方面学到的东西。我发现“错误消息中最相关的部分正在说明”是这样的。
db.proc_GetFramingSystems(brandID, frameType, glazeMethod, getMin).ToList<IFramingSystem>();
is expecting a column to be returned from the stored procedure with the alias of 'FrameType'.
期望从存储过程返回一个别名为“FrameType”的列。
I ran into this when I created a POCO (plain old clr object) class of my table with more programmer friendly names. Instead of a strongly typed name of say 'email_address', I wanted 'EmailAddy' and so forth. I created a mapped class stating this among other mapped columns.
当我使用更程序员友好的名称创建表的 POCO(普通旧 clr 对象)类时,我遇到了这个问题。我想要的不是“email_address”这样的强类型名称,而是“EmailAddy”等等。我创建了一个映射类,在其他映射列中说明了这一点。
this.Property(t => t.EmailAddy).HasColumnName("email_address");
Although this is necessary for other parts of EF to work, the mapping class is not referenced when executing a db.SqlQuery. So, when the code below executes
尽管这对于 EF 的其他部分工作是必要的,但在执行 db.SqlQuery 时不会引用映射类。所以,当下面的代码执行时
var a = new SqlParameter("@fshipno", shipno);
return _context.db.SqlQuery<EmailList>("exec spGetEmailAddy @fshipno", a).ToList();
It generated the same error except instead of 'FrameType', it mentioned 'EmailAddy'. The fix... I had to alias the 'email_address' column in my stored procedure to 'EmailAddy' to get it to map the returned dataset to my POCO.
它生成了相同的错误,除了“FrameType”之外,它提到了“EmailAddy”。修复...我必须将存储过程中的“email_address”列别名为“EmailAddy”,以便将返回的数据集映射到我的 POCO。
EDIT: I have found that this only works if your method is returning an IEnumberable
编辑:我发现这仅在您的方法返回 IEnumberable 时才有效
public IEnumberable<myPOCO> GetMyPoco()
You will get the same error message if you are attempting to return a single POCO object.
如果您尝试返回单个 POCO 对象,您将收到相同的错误消息。
public myPOCO GetMyPoco()