Skip to content

Instantly share code, notes, and snippets.

@michelmattos
Last active August 29, 2015 14:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michelmattos/a18ad7ac6b9fad2431a7 to your computer and use it in GitHub Desktop.
Save michelmattos/a18ad7ac6b9fad2431a7 to your computer and use it in GitHub Desktop.
How to "seed" an user using migrations with ASP.net MVC 5
namespace Identity.Migrations
{
using Identity.Models;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using System;
using System.Data.Entity;
using System.Data.Entity.Migrations;
using System.Linq;
internal sealed class Configuration : DbMigrationsConfiguration<Identity.Models.ApplicationDbContext>
{
public Configuration()
{
AutomaticMigrationsEnabled = true;
ContextKey = "Identity.Models.ApplicationDbContext";
}
protected override void Seed(Identity.Models.ApplicationDbContext context)
{
if (!context.Users.Any(u => u.UserName == "user@test.com"))
{
var store = new UserStore<ApplicationUser>(context);
var manager = new UserManager<ApplicationUser>(store);
var user = new ApplicationUser { UserName = "user@test.com" };
manager.Create(user, "password");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment