Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to change the identity table name and then customize user and role #2

Open
rakib33 opened this issue Dec 1, 2014 · 6 comments
Open

Comments

@rakib33
Copy link

rakib33 commented Dec 1, 2014

I follow your AspNet-Identity-2-Extending-Users-And-Roles tutorial and it working fine .But i wanted to change my Identity table name.I wrote the code as below
public class ApplicationDbContext : IdentityDbContext
{
public ApplicationDbContext()
: base("DefaultConnection")
{

    }
    protected override void OnModelCreating(System.Data.Entity.DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
        modelBuilder.Entity<IdentityUser>().ToTable("user");
        modelBuilder.Entity<ApplicationUser>().ToTable("user");

        modelBuilder.Entity<IdentityRole>().ToTable("role");
        modelBuilder.Entity<IdentityUserRole>().ToTable("userrole");
        modelBuilder.Entity<IdentityUserClaim>().ToTable("userclaim");
        modelBuilder.Entity<IdentityUserLogin>().ToTable("userlogin");
    }
}

it changes identity table name.but create a problem in userroles table an extra column added [IdentityUser_Id] NVARCHAR (128) NULL, and when i add user and user role no data added in this field .and [authorize(Role="Admin,User")] always return log in page .how can it solved ?please give suggession? the userroles table script is below:
CREATE TABLE [dbo].[userrole]([UserId] NVARCHAR %28128%29 NOT NULL,
[RoleId] NVARCHAR %28128%29 NOT NULL,
[IdentityUser_Id] NVARCHAR %28128%29 NULL,
CONSTRAINT [PK_dbo.userrole] PRIMARY KEY CLUSTERED %28[UserId] ASC, [RoleId] ASC%29,
CONSTRAINT [FK_dbo.userrole_dbo.mcms_role_RoleId] FOREIGN KEY %28[RoleId]%29 REFERENCES [dbo].[role] %28[Role_ID]%29 ON DELETE CASCADE,
CONSTRAINT [FK_dbo.mcms_userrole_dbo.mcms_user_IdentityUser_Id] FOREIGN KEY %28[IdentityUser_Id]%29 REFERENCES [dbo].[user] %28[User_ID]%29);

@xivSolutions
Copy link
Contributor

I can't look at this right now, as I am out the door to work. However, if you change the name of a table (or any of the column names involved in Primary Keys/Foreign Keys, I believe you need to do some additional mapping so that EF knows how things relate.

The existing DB entities are mapped to eachother based on convention.

You have introduced a new table, and EF has no way to know how it fits in the scheme of things, so it does the best it can based on your model.

You will likely need to do something like I did when mapping two new tables into the database for the Group-Based Permisssions project.

@rakib33
Copy link
Author

rakib33 commented Dec 2, 2014

Dear Sir,
Thank you for replay.

On Mon, Dec 1, 2014 at 6:30 PM, John Atten [email protected] wrote:

I can't look at this right now, as I am out the door to work. However, if
you change the name of a table (or any of the column names involved in
Primary Keys/Foreign Keys, I believe you need to do some additional mapping
so that EF knows how things relate.

The existing DB entities are mapped to eachother based on convention.

You have introduced a new table, and EF has no way to know how it fits in
the scheme of things, so it does the best it can based on your model.

You will likely need to do something like I did when mapping two new
tables into the database for the Group-Based Permisssions
http://typecastexception.com/post/2014/08/10/ASPNET-Identity-20-Implementing-Group-Based-Permissions-Management.aspx#Override-OnModelCreating-in-the-DbContext-Class
project.


Reply to this email directly or view it on GitHub
#2 (comment)
.

@rakib33
Copy link
Author

rakib33 commented Dec 2, 2014

Dear john Atten,
I can solve it now.My mapping code is :
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);

        modelBuilder.Entity<ApplicationUser>().ToTable("User");

        modelBuilder.Entity<IdentityRole>().ToTable("Role");

        modelBuilder.Entity<IdentityUserRole>().ToTable("UserRole");
        modelBuilder.Entity<IdentityUserClaim>().ToTable("UserClaim");
        modelBuilder.Entity<IdentityUserLogin>().ToTable("UserLogin");

    }

On Tue, Dec 2, 2014 at 10:34 AM, Rakibul Islam [email protected]
wrote:

Dear Sir,
Thank you for replay.

On Mon, Dec 1, 2014 at 6:30 PM, John Atten [email protected]
wrote:

I can't look at this right now, as I am out the door to work. However, if
you change the name of a table (or any of the column names involved in
Primary Keys/Foreign Keys, I believe you need to do some additional mapping
so that EF knows how things relate.

The existing DB entities are mapped to eachother based on convention.

You have introduced a new table, and EF has no way to know how it fits in
the scheme of things, so it does the best it can based on your model.

You will likely need to do something like I did when mapping two new
tables into the database for the Group-Based Permisssions
http://typecastexception.com/post/2014/08/10/ASPNET-Identity-20-Implementing-Group-Based-Permissions-Management.aspx#Override-OnModelCreating-in-the-DbContext-Class
project.


Reply to this email directly or view it on GitHub
#2 (comment)
.

@amanbindal
Copy link

In exactly similar scenario, I am getting following error:
The model backing the 'ApplicationDbContext' context has changed since the database was created. Consider using Code First Migrations to update the database (http://go.microsoft.com/fwlink/?LinkId=238269).

Thanks in advance!!

@xivSolutions
Copy link
Contributor

Yeah, things have changed since the article was written. If any one wants to me the changes and shoot me a PR, I would be forever grateful :-)

@amanbindal
Copy link

Following is the code snippet, I have my existing User table from existing database and don't want roles and claim etx, Just username password authentication, So I've put ignore for everything I dont want, and I am getting above error:

modelBuilder.Entity()
.ToTable("User", "dbo").Property(p => p.Id).HasColumnName("UserId");

        modelBuilder.Entity<ApplicationUser>()
            .ToTable("User", "dbo").Property(p => p.Id).HasColumnName("UserId");

        modelBuilder.Entity<ApplicationUser>()
            .ToTable("User", "dbo").HasKey(key => key.Id);

        modelBuilder.Entity<ApplicationUser>()
            .ToTable("User", "dbo").Property(x => x.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);

        modelBuilder.Entity<ApplicationUser>()
            .ToTable("User", "dbo").Property(p => p.UserName).HasColumnName("UserName");

        modelBuilder.Entity<IdentityUser>()
            .ToTable("User", "dbo").Property(p => p.PasswordHash).HasColumnName("Password");

        modelBuilder.Entity<IdentityUser>()
            .ToTable("User", "dbo").Property(p => p.Email).HasColumnName("EmailID");

        modelBuilder.Entity<IdentityUser>()
            .ToTable("User", "dbo").Ignore(p => p.AccessFailedCount);

        modelBuilder.Entity<IdentityUser>()
            .Ignore(p => p.Roles);

        modelBuilder.Entity<IdentityUser>()
            .Ignore(p => p.Claims);

        modelBuilder.Entity<IdentityUser>()
            .Ignore(p => p.Logins);

        modelBuilder.Ignore<IdentityUserRole>();
        modelBuilder.Ignore<IdentityUserLogin>();
        modelBuilder.Ignore<IdentityUserClaim>();
        modelBuilder.Ignore<IdentityRole>();

Error:

The model backing the 'ApplicationDbContext' context has changed since the database was created. Consider using Code First Migrations to update the database

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants