使用EF我想从Dot数据库中删除一个用户......这一切都适用于像
这样的代码DbContext.ExecuteStoreCommand("DELETE FROM activeforums_Attachments where UserId = " + userId);
DbContext.ExecuteStoreCommand("DELETE FROM activeforums_Forums_Tracking where UserId = " + userId);
DbContext.ExecuteStoreCommand("DELETE FROM activeforums_Poll where UserId = " + userId);
DbContext.ExecuteStoreCommand("DELETE FROM activeforums_Poll_Results where UserId = " + userId);
我可以使用aspNetUserId手动删除:
string aspNetUserId = "'21F72BCA-A51P-47DF-B57A-C31A191C61D2'";
DbContext.ExecuteStoreCommand("DELETE FROM aspnet_Membership where UserId = " + aspNetUserId);
DbContext.ExecuteStoreCommand("DELETE FROM aspnet_UsersInRoles where UserId = " + aspNetUserId);
DbContext.ExecuteStoreCommand("DELETE FROM aspnet_Users where UserId = " + aspNetUserId);
这有效...所以现在我需要做的就是从userName获取每个人的aspNetUserId ...所以我正在尝试像
这样的东西using (DEntities DbContext = DAOHelper.GetObjectContext<DEntities>())
{
var userIdToRemove = DbContext.ExecuteStoreCommand("Select FROM aspnet_Membership where UserName = " + userName);
DbContext.ExecuteStoreCommand("DELETE FROM aspnet_Membership where UserId = " + userIdToRemove.UserId);
DbContext.ExecuteStoreCommand("DELETE FROM aspnet_UsersInRoles where UserId = " + userIdToRemove.UserId);
DbContext.ExecuteStoreCommand("DELETE FROM aspnet_Users where UserId = " + userIdToRemove.UserId);
这样的事情可能吗?在使用EF时设置var,如果没有其他任何想法如何解决这个问题...感谢任何回复
答案 0 :(得分:1)
看起来您的选择查询错误:
Select FROM aspnet_Membership where UserName = userName
应该是
Select UserId FROM aspnet_Membership where UserName = userName
因为您只需要UserId。
另外,您应该使用ExecuteStoreQuery来查询数据库,如下所示:
var userIdToRemove = DbContext.ExecuteStoreQuery<string>("Select UserId FROM aspnet_Membership where UserName = " + userName);
然后当然使用userIdToRemove而不是userIdToRemove.UserId