Convert stored procedure result to model object

前端 未结 1 962
执笔经年
执笔经年 2021-01-22 09:02

I have a stored procedure In my asp.net mvc3 application using Entity Framework:

CREATE PROCEDURE dbo.MinMax
AS

DECLARE @T1 TABLE(MinColorsId int,MAXColorsId in         


        
1条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-22 09:46

    First of all, you can simplify your stored procedure to be:

    CREATE PROCEDURE dbo.MinMax
    AS
       SELECT  
           MIN(ColorsId) AS MinColorsId,
           MAX(ColorsId) AS MaXColorsId
       FROM dbo.DiamondInfoes 
    

    There's absolutely no need nor any point in having that table variable - it only puts strain on your TempDB.

    Next - if you're on .NET 4.0 and using the database-first approach - you can import that stored procedure into your EDMX model from the wizard:

    enter image description here

    Next, go to the Model Browser in Visual Studio and find your newly imported stored procedure in the physical "store" section of your model. On your stored procedure, right-click and pick Add Function Import:

    enter image description here

    Now a dialog box pops up which gives you the opportunity to pick what this stored procedure returns - in your case, you said you already have a type that represents that result - so in your case, you should find this type in the dropdown for Entities - pick it and close the dialog:

    enter image description here

    You should now have a method on your ObjectContext-derived class that represents this stored procedure, and returns an entity of your chosen type.

    0 讨论(0)
提交回复
热议问题