I want create delete user by userid using a stored procedure in Entity Framework code-first. But I am new to Entity Framework so I don't know how can do that this task. Can anyone please let me know how I can do that?
This is my code so far:
Stored procedure in SQL Server:
CREATE PROCEDURE [dbo].[DeleteUserById]
@UserId bigint
AS
BEGIN
DELETE FROM [dbo].[Users]
WHERE id = @UserId
END
Now now can this stored procedure be used in code? I don't know any one please tell me.
This is my class model builder :
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext() : base("DefaultConnection")
{
}
public virtual DbSet<UserInfo> UserInfo { get; set; }
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
public virtual ObjectResult<List<Users>> GetUsers()
{
return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction<List<UserInfo>>("GetUsers");
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Users>()
.MapToStoredProcedures(s => s.Delete(u => u.HasName("DeleteUserById", "dbo")
.Parameter(b => b.id, "userid"))
);
}
}
This is my delete method in the controller :
public string DeleteUsers(int id)
{
//here how can call this sp and delete this user
}
If anyone knows, then please help me with this task.