C# 如何首先使用实体框架代码调用存储过程?
我想先在实体框架代码中使用存储过程创建delete user by userid。但我对实体框架还不熟悉,所以我不知道如何才能完成这项任务。谁能告诉我怎么做 这是我目前的代码: SQL Server中的存储过程:C# 如何首先使用实体框架代码调用存储过程?,c#,sql-server,entity-framework,stored-procedures,C#,Sql Server,Entity Framework,Stored Procedures,我想先在实体框架代码中使用存储过程创建delete user by userid。但我对实体框架还不熟悉,所以我不知道如何才能完成这项任务。谁能告诉我怎么做 这是我目前的代码: SQL Server中的存储过程: CREATE PROCEDURE [dbo].[DeleteUserById] @UserId bigint AS BEGIN DELETE FROM [dbo].[Users] WHERE id = @UserId END 现在这个存储过程可以在代码
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"))
);
}
}
如果有人知道,请帮我完成这项任务。使用EF可以通过这种方式实现。Anf您不需要
MapToStoredProcess
public void DeleteUsers(int id)
{
SqlParameter idParam = new SqlParameter("@UserId", id);
context.Database.ExecuteSqlCommand("DeleteUserById @UserId", idParam);
}
编辑:并且您不会从存储过程返回任何内容,因此方法符号应该是
void
使用EF可以通过这种方式实现。Anf您不需要MapToStoredProcess
public void DeleteUsers(int id)
{
SqlParameter idParam = new SqlParameter("@UserId", id);
context.Database.ExecuteSqlCommand("DeleteUserById @UserId", idParam);
}
编辑:并且您不会从存储过程返回任何内容,因此方法符号应该是
void
您可以使用数据库上的ExecuteSqlCommand方法执行此操作
this.Database.ExecuteSqlCommand(
"[dbo].[DeleteUserById] @UserId",
new SqlParameter("@UserId", id));
更新
(对评论的答复)
您可以使用数据库上的ExecuteSqlCommand方法执行此操作
this.Database.ExecuteSqlCommand(
"[dbo].[DeleteUserById] @UserId",
new SqlParameter("@UserId", id));
更新
(对评论的答复)
描述了两种可能的方法,以及它们的优点。但是在这段代码中我该怎么做呢?写这段代码是正确的还是错误的??你可以用实体框架linq命令删除用户。谢谢,这也是wave,但我想需要sp call,以便您可以了解sp,然后让我知道描述了两种可能的方法,以及它们的优点。但是在这段代码中,我如何才能做到这一点?这段代码是正确的还是错误的?您可以使用entity framework linq命令删除用户。谢谢,这也是wave,但我想需要sp调用,以便您可以了解sp,然后让我知道sp_MyStoredProc这里回复我的sp名称??正如我可以在类中定义的那样?我想在存储中需要一个新类,只需设置userid??没有完全理解您最后一个问题在模型创建时受保护覆盖void(DbModelBuilder modelBuilder){modelBuilder.Entity().maptostoredprocesses(s=>s.Delete(u=>u.HasName(“DeleteUserById”,“dbo”)。参数(b=>b.id,“userid”));}我在不显示我的用户列表后编写了此代码,我遇到了错误,如何修复它?就像EntityType“IdentityUserRole”没有定义键一样。定义此EntityType的键。错误是gettingsp_MyStoredProc here回复我的sp名称??正如我在类中定义的那样?我想在存储中需要一个新的类just userid get set??没有完全捕获您的上一个问题在ModelCreating(DbModelBuilder modelBuilder){modelBuilder.Entity().MapToStoredProcedures(s=>s.Delete)上保护覆盖无效(u=>u.HasName(“DeleteUserById”,“dbo”)。参数(b=>b.id,“userid”);}我在没有显示我的用户列表后编写此代码我遇到了错误,那么如何修复它?就像此EntityType“IdentityUserRole”没有定义键一样。定义此EntityType的键。错误是getting我编写此代码并添加OnModelCreating方法,然后转到登录错误获取无效的对象名称“dbo.ApplicationUsers”。您能w可以很好地修复它,您的表名为
dbo.Users
,但它正在查找dbo.ApplicationUsers
请尝试将表(“用户”)属性应用于ApplicationUsers`类。意思是idk请给我提示如何处理这是我的公共类ApplicationUser:IdentityUser{公共异步任务GenerateUserIdentityAsync(UserManager管理器,字符串authenticationType){//注意authenticationType必须与CookieAuthenticationOptions.authenticationType var userIdentity=Wait manager.CreateIdentityAsync(此,authenticationType)中定义的类型匹配;//在此处添加自定义用户声明返回userIdentity;}仍在获取错误实体类型“Users”和“ApplicationUser”无法共享表“Users”,因为它们不在同一类型层次结构中,或者它们之间没有有效的一对一外键关系以及匹配的主键。我正在编写此代码并添加OnModelCreating方法,然后转到登录获取错误无效对象名称“dbo.ApplicationUsers”。您能告诉我如何修复它吗?您的表名为dbo.Users
,但它正在查找dbo.ApplicationUsers
请尝试将表(“Users”)属性应用于ApplicationUsers`类。意思是idk请给我提示这是我的公共类ApplicationUser:IdentityUser{公共异步任务GenerateUserIdentityAsync(UserManager管理器,字符串authenticationType){//注意authenticationType必须与CookieAuthenticationOptions.authenticationType var userIdentity=Wait manager.CreateIdentityAsync(此,authenticationType)中定义的类型匹配;//在此处添加自定义用户声明返回userIdentity;}}}仍然出现错误实体类型“Users”和“ApplicationUser”无法共享表“Users”,因为它们不在同一类型层次结构中,或者它们之间没有有效的一对一外键关系,并且主键匹配。