CrazyLegs
CrazyLegs

Reputation: 608

Add user to aspnetuser table via another controller

I'm using C#, ASP.NET MVC and .NET 4.5. I have a test controller which only I can access, basically I want to be able to create test users of say 500 at a time and insert these into my aspnetusers table, this is on my test environment.

In my controller I have this:

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);

This goes off to the standard register controller:

public async Task<ActionResult> Register(RegisterViewModel model)

However, it won't register a user? It goes wrong at the CreateAsync part:

var user = new ApplicationUser { UserName = model.Email, Email = model.Email, AccountType = model.AccountType };
var result = await UserManager.CreateAsync(user, model.Password);

I have seen others mention about "seeding" this at the start but I don't want to do this, I've have loads of data I need to automate/add in once the user is in the system, so need to loop over each user entered then insert a load more data. I don't want to have to create an account for each manually and then have to add in all the other details in all the other tables also.

Surely there is a simple way to do this that somebody knows?..

Upvotes: 2

Views: 1348

Answers (1)

make your non-sync function like that...

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();
    }
}

Upvotes: 1

Related Questions