简体   繁体   中英

In Entity Framework How To Extract Procedure Information In mvc asp.net

I have a stored procedure in sql server ie

 Create  PROCEDURE [dbo].[Validate_User]
        @Username NVARCHAR(20),
        @Password NVARCHAR(20)
    AS
    BEGIN
        SET NOCOUNT ON;
        DECLARE @UserId INT, @LastLoginDate DATETIME, @RoleId INT

        SELECT @UserId = UserId, @LastLoginDate = LastLoginDate, @RoleId = RoleId 
        FROM Users WHERE Username = @Username AND [Password] = @Password

        IF @UserId IS NOT NULL
        BEGIN
            IF NOT EXISTS(SELECT UserId FROM UserActivation WHERE UserId = @UserId)
            BEGIN
                UPDATE Users
                SET LastLoginDate =  GETDATE()
                WHERE UserId = @UserId

                SELECT @UserId [UserId], 
                        (SELECT RoleName FROM Roles 
                         WHERE RoleId = @RoleId) [Roles] -- User Valid and Returns UserId and Roles
            END
            ELSE
            BEGIN
                SELECT -2 -- User not activated.
            END
        END
        ELSE
        BEGIN
            SELECT -1 -- User invalid.
        END
END

Here is my ActionResult

[HttpPost]
public ActionResult Login(string Username,string Password,bool RememberMe)
{
            try
            {
                int userid = 0;
                string roles = string.Empty;
                RememberMe = false;
                if(ModelState.IsValid)
                {
                     //Here I wnat to extract the procedure info like
                     // UserId And Roles if valid user 
                     // And set those information in userid and roles
                     //...
                }
            }catch(Exception ex){
                 //Handel Any Exception
            }
}

I am using Entity Framework Model but I am not getting any idea regarding how to extract the Procedure Return Values using EF like we extract info using SqlDataReader as normal way with indexing eg reader["UserId"] or reader["Roles"] . So how will i do that in mvc with EF ?

Ok, well

First : go the project in the same solution where you have Your Entities

Second : go to the Model Browser Tab

Third : rigth click in any space of the diagram and click on Update Model

Fourth : follow the wizard steps, then you select the store procedures you need

Fifth : Once you add them, now look for the folder in the same tab "Functions Imports". Add one and name it and link it with the SP you already added

Remember that you have to create a complex type that are the columns your SP will return, ok?

Now go to the controller class. Remember to add the Model Context Namespace at the top of the class

Now when you create a new instance of the model context you will see the spFunctionImport that you named it

https://msdn.microsoft.com/en-us/library/bb896231(v=vs.100).aspx

the following link is old, but is more graphic

https://msdn.microsoft.com/en-us/data/gg699321.aspx

Regards

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM