我正在使用MVC4和VS_2012。我想删除一个用户我的管理面板。 我通常会这样删除;
UsersContext dcUser = new UsersContext();
if (userId.HasValue)
{
var userToDelete = (from u in dcUser.UserProfiles
where u.UserId == userId
select u).Single();
// dcUser.UserProfiles.DeleteOnSubmit(userToDelete);
// dcUser.SubmitChanges();
}
但是这种情况我无法访问DeleteOnSubmit和SubmitChanges。 有没有办法删除用户?
答案 0 :(得分:0)
试试这样:
dcUser.Entry<string>(userToDelete).State = System.Data.EntityState.Deleted;
答案 1 :(得分:0)
你可以试试这个:
[Authorize(Roles = "Admin")]
[HttpPost]
public ActionResult DeleteUser(int id)
{
var tmpuser = "";
var ctx = new UsersContext();
using (ctx)
{
var firstOrDefault = ctx.UserProfiles.FirstOrDefault(us => us.UserId==id);
if (firstOrDefault != null)
tmpuser = firstOrDefault.UserName;
}
string[] allRoles = Roles.GetRolesForUser(tmpuser);
Roles.RemoveUserFromRoles(tmpuser,allRoles);
((SimpleMembershipProvider)Membership.Provider).DeleteAccount(tmpuser);
Membership.Provider.DeleteUser(tmpuser, true);
Membership.DeleteUser(tmpuser, true);
ctx = new UsersContext();
return View(ctx.UserProfiles.OrderBy(user => user.UserName).ToList());
}