我的数据库(StarterSite)中有一个表(About_User),该表应该可以工作,以便每个单独的行都针对每个单独的用户。但是,无论哪个用户登录,他们都可以访问同一行(换句话说,不会为每个新用户创建新行)。我进行了三次检查,以确保数据库中的所有内容都设置正确。我在此注册页面中使用 StarterSite 模板(针对问题进行编辑):
// If all information is valid, create a new account
if (Validation.IsValid()) {
// Insert a new user into the database
var db = Database.Open("StarterSite");
// Check if user already exists
var user = db.QuerySingle("SELECT Email FROM UserProfile WHERE LOWER(Email) = LOWER(@0)", email);
if (user == null) {
// Insert email into the profile table
db.Execute("INSERT INTO UserProfile (Email) VALUES (@0)", email);
// Create and associate a new entry in the membership database.
// If successful, continue processing the request
try {
bool requireEmailConfirmation = !WebMail.SmtpServer.IsEmpty();
var token = WebSecurity.CreateAccount(email, password, requireEmailConfirmation);
if (requireEmailConfirmation) {
var hostUrl = Request.Url.GetComponents(UriComponents.SchemeAndServer, UriFormat.Unescaped);
var confirmationUrl = hostUrl + VirtualPathUtility.ToAbsolute("~/Account/Confirm?confirmationCode=" + HttpUtility.UrlEncode(token));
WebMail.Send(
to: email,
subject: "Please confirm your account",
body: "Your confirmation code is: " + token + ". Visit <a href=\"" + confirmationUrl + "\">" + confirmationUrl + "</a> to activate your account."
);
}
if (requireEmailConfirmation) {
// Thank the user for registering and let them know an email is on its way
Response.Redirect("~/Account/Thanks");
} else {
// Navigate back to the homepage and exit
WebSecurity.Login(email, password);
Response.Redirect("~/");
}
} catch (System.Web.Security.MembershipCreateUserException e) {
ModelState.AddFormError(e.Message);
}
} else {
// User already exists
ModelState.AddFormError("Email address is already in use.");
}
}
该表确实有一个(应该)自动增量的 ID 列。
其他列来自从用户收集不同信息的表单,例如“关于我”页面,因此每个人的信息都应该不同。
我怎样才能做到这一点?
最佳答案
你的问题中有一些我不明白的地方。首先,您的代码看起来没有经过编辑(它与第 39 行到第 82 行的模板相同)。其次,您的问题有 mysql 标签:如果您使用的是 MySql 数据库而不是 Starter Site 模板的 Sql Ce 数据库,也许这可能是问题所在(查看 WebSecurity.CreateAccount fails for MySQL )。
使用默认的 StarteSite Sql Ce 数据库,您的问题的答案非常简单。添加以下行
db.Execute("INSERT INTO About_User(UserId, UserPage) VALUES (@0, @1)",
db.GetLastInsertId(), userPage);
就在 db.Execute("INSERT INTO UserProfile (Email) VALUES (@0)", email);
语句之后。
About_User 表中不需要 ID 自动增量列,只需要 UserId 主键列作为外键。
关于mysql - 为每个新用户更新数据库表中的行 WebMatrix,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23964604/