1

How can I use ASP.NET Identity with custom table structure as User (UserId,Password) and UserRole (UserId,IsAdmin,IsNormalUser,IsManager) With MVC 5? I don't want to use all default tables like AspNetUsers, AspNetRoles, AspNetUserClaims, AspNetUserRoles, AspNetUserLogins ?

varsha
  • 1,620
  • 1
  • 16
  • 29
  • Maybe you can make a custom interface, here is an example: http://www.asp.net/identity/overview/extensibility/overview-of-custom-storage-providers-for-aspnet-identity – Aristos Jan 16 '15 at 09:25
  • Thanks Aristos! But still that example have all the tables same as AspNet tables , what my question is I have only two tables as described above and no other columns. – Manoj Prajapati Jan 16 '15 at 10:25

1 Answers1

1

Use the OnModelCreating event to map the tables and/or columns to your own schema as I have done in this StackOverflow question.

This will look something like-

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

    var user = modelBuilder.Entity<IdentityUser>().HasKey(u => u.Id).ToTable("User", "Users"); //Specify our our own table names instead of the defaults

    user.Property(iu => iu.Id).HasColumnName("UserId");
    user.Property(iu => iu.UserName).HasColumnName("UserName");
    user.Property(iu => iu.PasswordHash).HasColumnName("Password");
    user.Ignore(iu => iu.Email);
    user.Ignore(iu => iu.EmailConfirmed);
    user.Ignore(iu => iu.PhoneNumber);
    user.Ignore(iu => iu.PhoneNumberConfirmed);
    user.Ignore(iu => iu.AccessFailedCount);
    user.Ignore(iu => iu.LockoutEnabled);
    user.Ignore(iu => iu.LockoutEndDateUtc);
    user.Ignore(iu => iu.SecurityStamp);
    user.Ignore(iu => iu.TwoFactorEnabled);

    user.HasMany(u => u.Roles).WithRequired().HasForeignKey(ur => ur.UserId);

    user.Ignore(iu => iu.Logins);
    user.Ignore(iu => iu.Claims);

    modelBuilder.Ignore<IdentityUserClaim)();

    modelBuilder.Ignore<IdentityUserLogin>();

...

}

The key parts of the fluent API here are the ToTable([TableName], [SchemaName]) and HasColumnName([ColumnName])

This can be adapted to either create the table for you using standard Entity Framework Code First, or map to an existing database or one you create manually using SQL scripts.

See Code First to a New Database for more on Code First (including Fluent API mapping)

As you are wanting to ignore properties of the DbContext classes and exclude some DbContext classes from being mapped altogether you will want to use the .Ignore() extension method for unmapped properties and .Ignore<T>() extension method of the model builder for the classes.

These properties (and collections) will still exist in memory, but will not be persisted to the database.

Community
  • 1
  • 1
pwdst
  • 13,909
  • 3
  • 34
  • 50
  • Thanks, But still I am not able to get it work as it requires tables like claims,roles what my question is simple I have two table User and UserRole exactly described above with no any other fields. – Manoj Prajapati Jan 19 '15 at 06:59
  • @ManojPrajapati This wasn't clear from your question, only your comment. I have updated the answer to show how you can avoid mapping unwanted tables and columns. – pwdst Jan 19 '15 at 13:28
  • I tried doing this to avoid the AspNetUserLogins table but it does not work. Certain functions (like FindByNameAsync) told me the Logins navigational property did not exist. I would have had to write my own functions and override the default ones which is not worth the time; I'd rather have the table even if it's not being used. – Brad Aug 09 '16 at 12:37