Entity Framework Seeding with Identity (Microsoft.Owin.Security) user

asp.net-identity asp.net-mvc entity-framework entity-framework-6

Question

I have a class that adds 2 users with roles and special fields to the database as seed. It saves the data in [dbo], which is my issue. substitute [AspNetUsers] for [dbo]. [IdentityUsers]. There are two created tables. The data are seeded and then enter AspNetUser. The information enters IdentityUser when I activate the website and register a new user.

The Migration class is as follows:

internal sealed class Configuration : DbMigrationsConfiguration<DatabaseContext>
{
    public Configuration()
    {
        AutomaticMigrationsEnabled = true;
    }

    protected override void Seed(DatabaseContext context)
    {
        base.Seed(context);

        var userStore = new UserStore<ApplicationUser>();
        var manager = new UserManager<ApplicationUser>(userStore);

        var role = new IdentityUserRole { Role = new IdentityRole(Model.Roles.ADMINISTRATOR) };
        var user = new ApplicationUser() { UserName = "123123", Email = "123123@123.com", Language = "en-US"};
        user.Roles.Add(role);
        IdentityResult result = manager.Create(user, "123123");

        var role2 = new IdentityUserRole { Role = new IdentityRole(Model.Roles.NORMAL) };
        var user2 = new ApplicationUser() { UserName = "qweqwe", Email = "qweqwe@qweqwe.com", Language = "fr-CA" };
        user.Roles.Add(role2);
        IdentityResult result2 = manager.Create(user2, "qweqwe");
    }
}

Here is the Identity model custom field definition for the ApplicationUser class.

public class ApplicationUser : IdentityUser, ICurrentUser
{
    public ApplicationUser()
    {
        Email = "";
        Language = "";
    }
    public string UserId {
        get { return base.Id; }
        set{} 
    }
    public string Email { get; set; } 
    public string Language { get; set; } 

}

The Entity Framework configuration class for this new class can be found right here.

public class ApplicationUserConfiguration : EntityTypeConfiguration<ApplicationUser>
{
    public ApplicationUserConfiguration()
    {
        this.HasKey(d => d.Id);
        this.Ignore(d => d.UserId);
    }
}

enter image description here

Both employ the same settings for the save DataContext:

 protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        //... others entity here

        modelBuilder.Configurations.Add(new ApplicationUserConfiguration());

        //Theses configuration are required since custom fields are added to ApplicationUser. Here is why : http://stackoverflow.com/questions/19474662/map-tables-using-fluent-api-in-asp-net-mvc5-ef6
        modelBuilder.Entity<IdentityUserLogin>().HasKey(l => l.UserId);
        modelBuilder.Entity<IdentityRole>().HasKey(r => r.Id);
        modelBuilder.Entity<IdentityUserRole>().HasKey(r => new { r.RoleId, r.UserId });

    }

Why do the names of my AspNet prefixe tables and Identity tables overlap, I wonder? Why does the web application utilize one while the seed uses the other?

1
5
12/24/2013 1:18:05 AM

Accepted Answer

ZZZ_tmp
6
12/24/2013 2:20:41 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow