EF調用存儲過程查詢表中的部分字段,報數據讀取器與指定的“AdventureWorksDWModel.Student”不兼容。某個類型為“Age”的成員在同名的數據讀取器中沒有對應的列。


實現功能:查詢單張表Student中返回指定的列

一:數據庫表結構:

二:存儲過程:

 1 USE [AdventureWorksDW]
 2 GO
 3 /****** Object:  StoredProcedure [dbo].[GetAllStudentInfo]    Script Date: 2014/11/18 21:47:36 ******/
 4 SET ANSI_NULLS ON
 5 GO
 6 SET QUOTED_IDENTIFIER ON
 7 GO
 8 -- =============================================
 9 -- Author:    王光旭
10 -- Create date: 2014-11-18
11 -- Description:    返回Student表中指定的字段
12 -- =============================================
13 ALTER PROCEDURE [dbo].[GetAllStudentInfo]
14     @stuName varchar(50)
15 AS
16 BEGIN
17     SET NOCOUNT ON;
18     select ID,Name,TID from Student        --注意此處沒有查表中的Age字段
19 END

三:EF模型更新表和存儲過程以及存儲過程的函數導入

四:客戶端調用

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Data.Objects;
 6 
 7 namespace ClassLibrary1
 8 {
 9     public class Class1
10     {
11         public void accp()
12         {
13             awdEntities awd = new awdEntities();
14             
15             ObjectParameter[] para = new ObjectParameter[] 
16             { 
17                 new ObjectParameter("stuName", "田三")
18             };
19             //QueryAllStudentInfo為導入存儲過程制定的那個函數名稱
20             var list = awd.ExecuteFunction<Student>("QueryAllStudentInfo", para).ToList();
21         }
22     }
23 }

此時問題就出來了:

解決辦法:

此時客戶端調用需要更改一下返回的數據類型:

 1 using System.Text;
 2 using System.Data.Objects;
 3 
 4 namespace ClassLibrary1
 5 {
 6     public class Class1
 7     {
 8         public void accp()
 9         {
10             awdEntities awd = new awdEntities();
11             
12             ObjectParameter[] para = new ObjectParameter[] 
13             { 
14                 new ObjectParameter("stuName", "田三")
15             };
16             //QueryAllStudentInfo為導入存儲過程制定的那個函數名稱
17             //之前的數據返回類型Student更改為QueryAllStudentInfo_Result
18             var list = awd.ExecuteFunction<QueryAllStudentInfo_Result>("QueryAllStudentInfo", para).ToList();
19         }
20     }
21 }

問題就到此解決完畢。希望能幫到大家。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM