我正在使用C#,ASP.NET MVC和.NET 4.5。我有一个只能访问的测试控制器,基本上,我希望能够一次创建500个测试用户,并将它们插入我的测试环境中的aspnetusers
表中。
在我的控制器中,我有这个:
RegisterViewModel model = new RegisterViewModel();
var s = Guid.NewGuid().ToString();
model.AccountType = accountType;
model.Email = s + "@email.com";
model.Password = s;
model.ConfirmPassword = s;
var result = new AccountController().Register(model);
这将转到标准寄存器控制器:
public async Task<ActionResult> Register(RegisterViewModel model)
但是,它不会注册用户吗?在CreateAsync部分出错:
var user = new ApplicationUser { UserName = model.Email, Email = model.Email, AccountType = model.AccountType };
var result = await UserManager.CreateAsync(user, model.Password);
我一开始曾看到其他人提到过“播种”,但是我不想这样做,因为一旦用户进入系统,我就需要自动加载/添加大量数据,因此需要循环输入的每个用户,然后加载更多数据。我不想手动为每个帐户创建一个帐户,然后也不必在所有其他表中添加所有其他详细信息。
肯定有一种简单的方法,有人知道吗?。
答案 0 :(得分:0)
使您的非同步功能...
public class TestController : Controller
{
ApplicationDbContext db = new ApplicationDbContext();
// GET: Test
public ActionResult Index()
{
var userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(db));
string s = "User!1Nom"; // must contain special char if you use asp.net identity default validation
for (int i = 1; i < 501; i++)
{
s = s + i;
var user = new ApplicationUser { UserName = s, Email = s+"@gmail.com" };
var result = userManager.Create(user, s);
}
return View();
}
}