Last active
August 29, 2015 14:11
-
-
Save michelmattos/a18ad7ac6b9fad2431a7 to your computer and use it in GitHub Desktop.
How to "seed" an user using migrations with ASP.net MVC 5
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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