我想通过使用实体框架中的存储过程的代码首先创建删除用户。但是我对Entity Framework很陌生,所以我不知道如何做到这一点。任何人都可以让我知道我该怎么做?如何使用实体框架代码优先调用存储过程?
这是我到目前为止的代码:
存储在SQL Server程序:
CREATE PROCEDURE [dbo].[DeleteUserById]
@UserId bigint
AS
BEGIN
DELETE FROM [dbo].[Users]
WHERE id = @UserId
END
现在现在可以此存储过程可以在代码中使用?我不知道任何人请告诉我。
这是我的类模型构造器:
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"))
);
}
}
这是我在控制器删除方法:
public string DeleteUsers(int id)
{
//here how can call this sp and delete this user
}
如果有人知道的话,请帮我完成这个任务。
[这个问题](http://stackoverflow.com/questions/643880/commandtype-text-vs-commandtype-存储过程)描述了两种可能的方式及其好处。 –
但在这段代码中,我该如何写这段代码是正确还是错误? – coderwill
您可以使用实体框架linq命令删除用户。 [检查此答案](http://stackoverflow.com/questions/31672899/how-do-i-delete-single-record-from-table-using-ef-6-1-1)。谢谢 –