c# - 数据读取器与指定的 Entity Framework 不兼容

标签 c# .net tsql entity-framework-4

我有一个方法可以从存储过程返回最小结果来填充选择菜单。当我想要最小结果时,我将 bool getMin = true 传递给存储过程,而当我想要完整记录时,我传递 bool getMin = false。

这导致 Entity Framework 错误“数据读取器与指定的不兼容

错误中最相关的部分

{"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",

显然错误告诉我,当数据读取器试图设置不在查询结果中的属性“FrameType”时。

现在我明白了这个错误,我想知道的是我是否要将这个 sql 存储过程分成两个存储过程,或者是否有解决这个问题的方法?

下面是我的函数

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>();
    };
}

下面是我的 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;

最佳答案

在我看来,IF 的两个分支都返回不同的数据,第一个分支返回 9 列,而第二个 - 只有三个。我相信 EF 无法反射(reflect)后者的 IFramingSystem。具体来说,FrameType 列(以及其他 5 列)显然缺失了:

 ...
 SELECT c.ID,c.Name,c.Descr    <- where are the remaining columns
    from Catelog.Component c
 ...

关于c# - 数据读取器与指定的 Entity Framework 不兼容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18791037/

相关文章:

c# - 如何在 LINQ 中用 Join 重写两个表的 SQL Union?

.net - 如何在 .NET 中找到路径分隔符?

SQL 按一定数量分组

tsql - T-SQL 小数除法精度

C# 多线程多类 gui 应用程序,我这样做对吗?

c# - 如何检查给定值是否为通用列表?

c# - WPF 中的可移植类库 ICommand 编译错误——不确定如何适本地解决这个问题?

C# - 导入 CSV,其中空字符串不会通过批量导入发送到数据库

.NET - 启用 UAC (Windows7/Vista) 时,应用程序如何能够 self 更新?

c# - 通过C#读取SQL Server中存储过程设置的值