Hasnat Adil
Hasnat Adil

Reputation: 29

The data reader is incompatible with the specified 'StoredProcedureReturn'

I write a stored procedure in which I have make JOIN and getting Data from Two tables.

I am getting Invigilator Name and also City name from JOIN in stored procedure but in Entity Framework Stored procedure is not running and Below exception is coming that

Message "The data reader is incompatible with the specified 'pts_dbnModel.GetInviglatorData_Result'. A member of the type, 'Name1', does not have a corresponding column in the data reader with the same name." string

select s.Name,s.contact ,c.Name ,s.frequency,s.cnic from zaid.invigilator s
INNER JOIN zaid.city c ON s.city=c.ID
INNER JOIN CompaignInvigilatorMapping m ON s.ID=m.InvigilatorId
INNER JOIN [hasnat.compaign] h ON m.CompaignId=h.ID where 
h.CompaignName=@CompName

Now the problem is on Name field . Because s.Name is from Invigilator Table and c.Name is from City table.

Upvotes: 0

Views: 558

Answers (2)

Aria
Aria

Reputation: 3844

You have two table with the same columns name there is no problem with your select query as you have done before, if you don't qualified column names(like s.Name,c.Name) you will get exception as

Ambiguous column name 'Name'.

but your problem is about EF, that error message means the result of your stored procedure do not contain a column named Name1, you have two option to resolve the problem.

1- Present Name1 in your stored procedure by alias name like:

select s.Name as Name1,s.contact ,c.Name ,s.frequency,s.cnic from zaid.invigilator s
INNER JOIN zaid.city c ON s.city=c.ID
INNER JOIN CompaignInvigilatorMapping m ON s.ID=m.InvigilatorId
INNER JOIN [hasnat.compaign] h ON m.CompaignId=h.ID where 
h.CompaignName=@CompName

2- Or make your EF model and query to be compatible with output of your stored procedure (for example rename Name1 to Name in EF model).

Your stored procedure don't have problem alone, but it should be compatible with your EF model or vice versa.

Upvotes: 1

Zaynul Abadin Tuhin
Zaynul Abadin Tuhin

Reputation: 32001

use alias

select s.Name as invaigilatorName,s.contact ,c.Name as cityName ,s.frequency,s.cnic from zaid.invigilator s
INNER JOIN zaid.city c ON s.city=c.ID
INNER JOIN CompaignInvigilatorMapping m ON s.ID=m.InvigilatorId
INNER JOIN [hasnat.compaign] h ON m.CompaignId=h.ID where 
h.CompaignName=@CompName

Upvotes: 1

Related Questions