I am trying to delete an already existing user from a database, which was created automatically when creating MVC application.
The database consists of tables:
我正在尝试从数据库中删除一个已经存在的用户,它是在创建MVC应用程序时自动创建的。数据库由表组成:
AspNetUsers
AspNetUserRoles
AspNetUserLogins
AspNetUserClaims
AspNetRoles
In my code it looks like this:
在我的代码中,它是这样的:
var user = new ApplicationUser { UserName = model.email, Email = model.email };
var context = new ApplicationDbContext();
context.Users.Attach(user);
context.Users.Remove(user);
context.SaveChangesAsync();
return RedirectToAction("OperationSuccess", "Account");
I have also tried this:
我也尝试过:
var user = new ApplicationUser { UserName = model.email, Email = model.email };
var context = new ApplicationDbContext();
UserManager.DeleteAsync(user);
But it doesn't help at all. The application itselt does not break and does not show any errors, but the user is still in the database. How do I delete it?
但这没有任何帮助。应用程序本身不会中断,也不会显示任何错误,但是用户仍然在数据库中。如何删除它?
3 个解决方案
#1
1
Try this code:
试试这段代码:
public async Task<IdentityResult> DeleteUser(string email)
{
var user = UserManager.Users.FirstOrDefault(x => x.Email == email);
if(user == null) return null;
var result = await UserManager.DeleteAsync(user); //here result has two properties Errors and Succeeded.
return result;
}
Also, your code is not working because you are creating the object yourself and assigning only two properties yourself in spite of fetching the data from database.
此外,您的代码不能工作,因为您正在自己创建对象,并且自己只分配两个属性,尽管从数据库中获取数据。
#2
0
Hi I think you have some versioning problem and its seems that you need to give one extra paramater to the DeleteAsync method.
嗨,我认为您有一些版本控制问题,似乎您需要为DeleteAsync方法提供一个额外的参数。
Kindly refer the below link, since they had same kind of issue and resolved it.
请参考下面的链接,因为他们有同样的问题并解决了。
https://*.com/a/24594440/3397630
https://*.com/a/24594440/3397630
Hope it may give you some idea for your solution too.
希望它也能给你一些解决方案的想法。
Thanks
谢谢
Karthik
恋人
#3
0
Hope below code will help you to fix your problem
希望下面的代码能帮助您解决问题
[HttpPost]
public async Task<ActionResult> Delete(string userId)
{
// Check for for both ID and exit if not found
if (String.IsNullEmpty(userId))
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
var user = UserManager.Users.SingleOrDefault(u => u.Id == Userid);// Look for user in the UserStore
// If not found, exit
if (user == null)
{
return HttpNotFound();
}
var results = await UserManager.DeleteAsync(user); // Remove user from UserStore
// If the statement is success
if (results.Succeeded)
{
// Redirect to Users page
return RedirectToAction("Index", "Users");
}
else
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
}
#1
1
Try this code:
试试这段代码:
public async Task<IdentityResult> DeleteUser(string email)
{
var user = UserManager.Users.FirstOrDefault(x => x.Email == email);
if(user == null) return null;
var result = await UserManager.DeleteAsync(user); //here result has two properties Errors and Succeeded.
return result;
}
Also, your code is not working because you are creating the object yourself and assigning only two properties yourself in spite of fetching the data from database.
此外,您的代码不能工作,因为您正在自己创建对象,并且自己只分配两个属性,尽管从数据库中获取数据。
#2
0
Hi I think you have some versioning problem and its seems that you need to give one extra paramater to the DeleteAsync method.
嗨,我认为您有一些版本控制问题,似乎您需要为DeleteAsync方法提供一个额外的参数。
Kindly refer the below link, since they had same kind of issue and resolved it.
请参考下面的链接,因为他们有同样的问题并解决了。
https://*.com/a/24594440/3397630
https://*.com/a/24594440/3397630
Hope it may give you some idea for your solution too.
希望它也能给你一些解决方案的想法。
Thanks
谢谢
Karthik
恋人
#3
0
Hope below code will help you to fix your problem
希望下面的代码能帮助您解决问题
[HttpPost]
public async Task<ActionResult> Delete(string userId)
{
// Check for for both ID and exit if not found
if (String.IsNullEmpty(userId))
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
var user = UserManager.Users.SingleOrDefault(u => u.Id == Userid);// Look for user in the UserStore
// If not found, exit
if (user == null)
{
return HttpNotFound();
}
var results = await UserManager.DeleteAsync(user); // Remove user from UserStore
// If the statement is success
if (results.Succeeded)
{
// Redirect to Users page
return RedirectToAction("Index", "Users");
}
else
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
}