我的数据库(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列。
其他列来自一个表单,它从用户那里收集不同的信息,比如“关于我”页面,因此每个人都应该是不同的。
我如何才能做到这一点呢?
发布于 2014-05-31 17:05:30
你的问题中有一些我不明白的地方。首先,您的代码看起来没有经过编辑(它与第39行到第82行的模板相同)。其次,你的问题有mysql标签:如果你使用的是MySql数据库而不是入门网站模板的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主键列作为外键。
https://stackoverflow.com/questions/23964604
复制相似问题