This repository has been archived by the owner. It is now read-only.

Changing Tables name #300

Closed
Shahrooze opened this Issue Dec 28, 2014 · 4 comments

Comments

Projects
None yet
5 participants
@Shahrooze
Copy link

Shahrooze commented Dec 28, 2014

I want to change Identity table name with this code:

     builder.Entity<ApplicationUser>(b =>
        {
            b.Key(u => u.Id);
            b.ForRelational().Table("Users");
        });
        builder.Entity<IdentityRole>(b =>
        {
            b.Key(r => r.Id);
            b.ForRelational().Table("Roles");
        });

        builder.Entity<IdentityUserClaim>(b =>
        {
            b.Key(uc => uc.Id);
            b.ManyToOne<ApplicationUser>().ForeignKey(uc => uc.UserId);
            b.ForRelational().Table("UserClaims");
        });

        builder.Entity<IdentityRoleClaim>(b =>
        {
            b.Key(rc => rc.Id);
            b.ManyToOne<IdentityUserRole>().ForeignKey(rc => rc.RoleId);
            b.ForRelational().Table("RoleClaims");
        });

        builder.Entity<IdentityUserRole>(b =>
        {
            b.Key(r => new { r.UserId, r.RoleId });
            b.ForRelational().Table("UserRoles");
        });

        builder.Entity<IdentityUserLogin>(b =>
        {
            b.Key(l => new { l.LoginProvider, l.ProviderKey });
            b.ManyToOne<ApplicationUser>().ForeignKey(uc => uc.UserId);
            b.ForRelational().Table("UserLogins");
        });

But this code not work for me when generate migration:

            builder.Entity("Microsoft.AspNet.Identity.IdentityRole", b =>
                {
                    b.Property<string>("Id");
                    b.Property<string>("Name");
                    b.Key("Id");
                    b.ForRelational().Table("Roles");
                });

            builder.Entity("Microsoft.AspNet.Identity.IdentityRoleClaim", b =>
                {
                    b.Property<string>("ClaimType");
                    b.Property<string>("ClaimValue");
                    b.Property<int>("Id")
                        .GenerateValuesOnAdd();
                    b.Property<string>("RoleId");
                    b.Key("Id");
                    b.ForRelational().Table("RoleClaims");
                });

            builder.Entity("Microsoft.AspNet.Identity.IdentityRoleClaim`1[[System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]", b =>
                {
                    b.Property<string>("ClaimType");
                    b.Property<string>("ClaimValue");
                    b.Property<int>("Id")
                        .GenerateValuesOnAdd();
                    b.Property<int>("RoleId");
                    b.Key("Id");
                    b.ForRelational().Table("AspNetRoleClaims");
                });

            builder.Entity("Microsoft.AspNet.Identity.IdentityUserClaim", b =>
                {
                    b.Property<string>("ClaimType");
                    b.Property<string>("ClaimValue");
                    b.Property<int>("Id")
                        .GenerateValuesOnAdd();
                    b.Property<string>("UserId");
                    b.Key("Id");
                    b.ForRelational().Table("UserClaims");
                });

            builder.Entity("Microsoft.AspNet.Identity.IdentityUserClaim`1[[System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]", b =>
                {
                    b.Property<string>("ClaimType");
                    b.Property<string>("ClaimValue");
                    b.Property<int>("Id")
                        .GenerateValuesOnAdd();
                    b.Property<int>("UserId");
                    b.Key("Id");
                    b.ForRelational().Table("AspNetUserClaims");
                });

            builder.Entity("Microsoft.AspNet.Identity.IdentityUserLogin", b =>
                {
                    b.Property<string>("LoginProvider");
                    b.Property<string>("ProviderDisplayName");
                    b.Property<string>("ProviderKey");
                    b.Property<string>("UserId");
                    b.Key("LoginProvider", "ProviderKey");
                    b.ForRelational().Table("UserLogins");
                });

            builder.Entity("Microsoft.AspNet.Identity.IdentityUserLogin`1[[System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]", b =>
                {
                    b.Property<string>("LoginProvider");
                    b.Property<string>("ProviderDisplayName");
                    b.Property<string>("ProviderKey");
                    b.Property<int>("UserId");
                    b.Key("LoginProvider", "ProviderKey");
                    b.ForRelational().Table("AspNetUserLogins");
                });

            builder.Entity("Microsoft.AspNet.Identity.IdentityUserRole", b =>
                {
                    b.Property<string>("RoleId");
                    b.Property<string>("RoleIdKey");
                    b.Property<string>("UserId");
                    b.Key("UserId", "RoleId");
                    b.ForRelational().Table("UserRoles");
                });

            builder.Entity("Microsoft.AspNet.Identity.IdentityUserRole`1[[System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]", b =>
                {
                    b.Property<int>("RoleId");
                    b.Property<int>("UserId");
                    b.Key("UserId", "RoleId");
                    b.ForRelational().Table("AspNetUserRoles");
                });

What is my problem?

@Shahrooze Shahrooze changed the title Changing Table name Changing Tables name Dec 28, 2014

@suhasj

This comment has been minimized.

Copy link
Contributor

suhasj commented Dec 29, 2014

Are you trying to change the Primary Key of the model classes from string to int ? In that case your builder mapping should be
builder.Entity<IdentityUserLogin<int>>(b => { b.Key(l => new { l.LoginProvider, l.ProviderKey }); b.ManyToOne<ApplicationUser>().ForeignKey(uc => uc.UserId); b.ForRelational().Table("UserLogins"); });
Try this for all models and let me know if that works for you

@Kukkimonsuta

This comment has been minimized.

Copy link

Kukkimonsuta commented Dec 30, 2014

On a side note, if you just want to remove AspNet prefix, this does to trick too:

protected override void OnModelCreating(ModelBuilder builder)
{
    base.OnModelCreating(builder);

    foreach (var entityType in builder.Model.EntityTypes)
    {
        var table = entityType.Relational().Table;
        if (table.StartsWith("AspNet"))
            entityType.Relational().Table = table.Substring(6);
    }
}
@rustd

This comment has been minimized.

Copy link

rustd commented Jan 5, 2015

Please reopen if this is still an issue.

@rustd rustd closed this Jan 5, 2015

@Bartmax

This comment has been minimized.

Copy link

Bartmax commented Mar 31, 2015

While removing AspNet prefix as @Kukkimonsuta suggested does work, I'm wondering why doing for every entity in a single line create 2 tables.

For example:

builder.Entity<IdentityRoleClaim>().ForRelational().Table("NewRoleClaim");

creates 2 tables: NewRoleClaim and AspnetRoleClaims.

why?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.