I am trying to learn Web API + Entity Framework. To use stored procedures for database access through Web API.
I've successfully done get call with select stored procedure. This is my code:
public class UsersController : ApiController
{
[Authorize]
public IEnumerable<spSelUsers_Result> GetUsers()
{
using(UsersEntities users = new UsersEntities())
{
return users.spSelUsers().ToList();
}
}
}
But I'm stuck here with insert procedure to create new record.
[Authorize]
[Route("AddUser")]
public class Users
{
public string user_name { get; set; }
public string first_name { get; set; }
public string last_name { get; set; }
public string mobile_no { get; set; }
public string email { get; set; }
public string user_pic { get; set; }
public int strowner { get; set; }
public DateTime dbtstamp { get; set; }
}
public IEnumerable<spInsUsers_Result> PostUsers([FromBody] UsersEntities Users)
{
using(UsersEntities Usrs = new UsersEntities())
{
return Usrs.spInsUsers();
}
}
From various tutorials online I managed to get up to here... I don't know how to assign values to parameters and pass them...
Also any suggestions on weather to use IEnumerable or anything else..
Currently I'm clueless...please help
Edit: 1
Stored Procedure
GO
ALTER PROCEDURE [dbo].[spInsUsers]
(
@user_name nvarchar(50),
@first_name nvarchar(50),
@last_name nvarchar(50),
@mobile_no varchar(10),
@email nvarchar(50),
@user_pic varchar(256),
@strowner int,
@dbtstamp datetime2
)
AS
SET NOCOUNT OFF;
INSERT INTO [tbl_users] ([user_name], [first_name], [last_name], [mobile_no], [email], [user_pic], [strowner], [dbtstamp]) VALUES (@user_name, @first_name, @last_name, @mobile_no, @email, @user_pic, @strowner, @dbtstamp);
SELECT user_id, user_name, first_name, last_name, mobile_no, email, user_pic, strowner, dbtstamp FROM tbl_users WHERE (user_id = SCOPE_IDENTITY())
SP Mapping:

User. So please show the stored procedure and also how you mapped theUserentity in your context class.