EF调用存储过程查询表中的部分字段,报数据读取器与指定的“AdventureWorksDWModel.Student”不兼容。某个类型为“Age”的成员在同名的数据读取器中没有对应的列。

时间:2025-05-05 14:35:14

实现功能:查询单张表Student中返回指定的列

一:数据库表结构:

EF调用存储过程查询表中的部分字段,报数据读取器与指定的“AdventureWorksDWModel.Student”不兼容。某个类型为“Age”的成员在同名的数据读取器中没有对应的列。

二:存储过程:

 USE [AdventureWorksDW]
GO
/****** Object: StoredProcedure [dbo].[GetAllStudentInfo] Script Date: 2014/11/18 21:47:36 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: 王光旭
-- Create date: 2014-11-18
-- Description: 返回Student表中指定的字段
-- =============================================
ALTER PROCEDURE [dbo].[GetAllStudentInfo]
@stuName varchar(50)
AS
BEGIN
SET NOCOUNT ON;
select ID,Name,TID from Student --注意此处没有查表中的Age字段
END

三:EF模型更新表和存储过程以及存储过程的函数导入

EF调用存储过程查询表中的部分字段,报数据读取器与指定的“AdventureWorksDWModel.Student”不兼容。某个类型为“Age”的成员在同名的数据读取器中没有对应的列。EF调用存储过程查询表中的部分字段,报数据读取器与指定的“AdventureWorksDWModel.Student”不兼容。某个类型为“Age”的成员在同名的数据读取器中没有对应的列。

EF调用存储过程查询表中的部分字段,报数据读取器与指定的“AdventureWorksDWModel.Student”不兼容。某个类型为“Age”的成员在同名的数据读取器中没有对应的列。

四:客户端调用

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Objects; namespace ClassLibrary1
{
public class Class1
{
public void accp()
{
awdEntities awd = new awdEntities(); ObjectParameter[] para = new ObjectParameter[]
{
new ObjectParameter("stuName", "田三")
};
//QueryAllStudentInfo为导入存储过程制定的那个函数名称
var list = awd.ExecuteFunction<Student>("QueryAllStudentInfo", para).ToList();
}
}
}

此时问题就出来了:

EF调用存储过程查询表中的部分字段,报数据读取器与指定的“AdventureWorksDWModel.Student”不兼容。某个类型为“Age”的成员在同名的数据读取器中没有对应的列。

解决办法:

EF调用存储过程查询表中的部分字段,报数据读取器与指定的“AdventureWorksDWModel.Student”不兼容。某个类型为“Age”的成员在同名的数据读取器中没有对应的列。EF调用存储过程查询表中的部分字段,报数据读取器与指定的“AdventureWorksDWModel.Student”不兼容。某个类型为“Age”的成员在同名的数据读取器中没有对应的列。

此时客户端调用需要更改一下返回的数据类型:

 using System.Text;
using System.Data.Objects; namespace ClassLibrary1
{
public class Class1
{
public void accp()
{
awdEntities awd = new awdEntities(); ObjectParameter[] para = new ObjectParameter[]
{
new ObjectParameter("stuName", "田三")
};
//QueryAllStudentInfo为导入存储过程制定的那个函数名称
//之前的数据返回类型Student更改为QueryAllStudentInfo_Result
var list = awd.ExecuteFunction<QueryAllStudentInfo_Result>("QueryAllStudentInfo", para).ToList();
}
}
}

问题就到此解决完毕。希望能帮到大家。