我正在检查将默认用户添加到MVC 5应用程序。这段代码工作正常,但是如何通过种子方法在数据库中更新这些数据?例如,如果我想更改电子邮件地址?或者将用户添加到角色?任何帮助赞赏。
if (!context.Users.Any(u => u.UserName == "someone@someplace.com"))
{
var store = new UserStore<ApplicationUser>(context);
var manager = new UserManager<ApplicationUser>(store);
var user = new ApplicationUser { UserName =
"someone@someplace.com",
Email = "someone@someplace.com",
EmailConfirmed = true };
manager.Create(user, "password");
}
答案 0 :(得分:1)
您可以直接从DbContext进行修改。您不需要致电身份库。这是一个例子。
using System.Data.Entity;
using System.Linq;
public class NameDbContextSeedData :
DropCreateDatabaseIfModelChanges<NameDbContext>
{
protected override void Seed(NameDbContext context)
{
var user = context.Users.Single(c => c.UserName ==
"someone@someplace.com");
user.UserName = "someone@someplace.com";
user.Email = "someone@someplace.com";
user.EmailConfirmed = true;
context.SaveChanges();
}
}