问题描述:

I am working on a migration project, where I am migrating an web forms app to a ASP.NET MVC 4 app.

I have come across this one scenario with which I need a little help.

In the old web form project I have stored proc like...

ALTER PROCEDURE [dbo].[TestMultipleResultSet]

@input VARCHAR

AS

BEGIN

SET NOCOUNT ON;

IF NOT EXISTS

(

SELECT * FROM Electronics

WHERE name = @input

)

BEGIN

SELECT 101 AS errorcode

RETURN

END

SELECT * FROM Electronics

WHERE name = @input

END

like the above if not exist check, there are many other checks with gives different select * from as resultset.

Now I want to use Entity framework to execute this stored procedure, so I imported the function and created a complex type using the Get Column Information + Create Complex Type option which EF provides.

Now it runs when I enter a valid @input which executes SELECT * FROM Electronics WHERE name = @input but when I enter an input which is not present instead of returning me a 101 error code it gives me an error. An exception is thrown.

Exception details

The data reader is incompatible with the specified 'testEFModel.GetTestResults_Result1'. A member of the type, 'id', does not have a corresponding column in the data reader with the same name.

How should I solve this ?

网友答案:

Is it really necessary that you use the stored procedure?

Using EF you could simply do:

var filteredItems = dc.Electronics.Where(e => e.Name == @input).ToList();

if (!filteredItems.Any())
    throw new ApplicationException("No items were found according to criteria");

Note: I have not actually run the above code; it just illustrates the idea doing things the EF way

相关阅读:
Top