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

Cannot use table 'AspNetRoles' for entity type 'AspNetRole' since it is being used for entity type 'IdentityRole' and potentially other entity types, but there is no linking relationship. #27251

Closed
adil-portable opened this issue Jan 22, 2022 · 12 comments

Comments

@adil-portable
Copy link

adil-portable commented Jan 22, 2022

File a bug

I am using Db first approach using latest EF version in DOTNET 6. It does not generate AspNetUserRole table and add dictionary details as

modelBuilder.Entity<AspNetUser>(entity =>
    {
        entity.HasIndex(e => e.UserName, "UserNameIndex")
            .IsUnique();

        entity.Property(e => e.Id).HasMaxLength(128);

        entity.Property(e => e.Email).HasMaxLength(256);

        entity.Property(e => e.LockoutEndDateUtc).HasColumnType("datetime");

        entity.Property(e => e.UserName).HasMaxLength(256);


        entity.HasMany(d => d.Roles)
            .WithMany(p => p.Users);
    .UsingEntity<Dictionary<string, object>>(
        "AspNetUserRole",
        l => l.HasOne<AspNetRole>().WithMany().HasForeignKey("RoleId").HasConstraintName("FK_dbo.AspNetUserRoles_dbo.AspNetRoles_RoleId"),
        r => r.HasOne<AspNetUser>().WithMany().HasForeignKey("UserId").HasConstraintName("FK_dbo.AspNetUserRoles_dbo.AspNetUsers_UserId"),
        j =>
        {
            j.HasKey("UserId", "RoleId").HasName("PK_dbo.AspNetUserRoles");

            j.ToTable("AspNetUserRoles");

            j.HasIndex(new[] { "RoleId" }, "IX_RoleId");

            j.HasIndex(new[] { "UserId" }, "IX_UserId");

            j.IndexerProperty<string>("UserId").HasMaxLength(128);

            j.IndexerProperty<string>("RoleId").HasMaxLength(128);
        });
    });

Now, when I am trying to use Microsoft.AspNetCore.Identity.EntityFrameworkCore method to get user details using FindByLoginAsync method. I get the error
image

My Program.cs is configured as:

image

I also tried by manually adding the table AspNetUserRole and adding the relationships as

image

Please download the project from https://drive.google.com/file/d/14zmk-jijpYPbyzwOkJ63OPBMM44CHkSY/view?usp=sharing

I would be grateful if you help as ASAP. Thanks

Include provider and version information

EF Core version: 6.0.1
Database provider: (e.g. Microsoft.EntityFrameworkCore.SqlServer)
Target framework: (e.g. .NET 6.0)
Operating system:
IDE: (e.g. Visual Studio 2019 16.3)

@ajcvickers
Copy link
Contributor

Duplicate of #26820. Also, this can be done now with the EF Core Power Tools,

@adil-portable
Copy link
Author

EF 6 does not include AspNetUserRoles DbSet and is not also create DbContextDiagram. I followed this https://www.youtube.com/watch?v=uph-AGyOd8c&ab_channel=dotNET but it is for EF 5. Please kindly help me ASAP.

Can I copy paste AspNetUserRoles class generated using EF 5 in EF 6 ? to make it work?
Thanks

@adil-portable
Copy link
Author

Even by using EF power tool for EF 6 I get AspNetUserRole relationship like
image
Goal is to get Role by user
image
DbContext is
image
I am inheriting
image

@ErikEJ
Copy link
Contributor

ErikEJ commented Feb 2, 2022

@adil-portable use the advanced option to actually generate an entity

@adil-portable
Copy link
Author

adil-portable commented Feb 3, 2022

Thank you very much @ErikEJ I am now able to generate AspNetUserRole object but I am still getting the same error:
Program.cs
image

service class
image

DbContext.cs
image
image

image

I tried a lot search on the internet to fix this problem but could not find a solution. I am using EF 6 and .NET 6.

I would be grateful if you could able to help.

Thanks once again

@grahamoriley
Copy link

I am getting the same error as you: adil-portable in a web .net 6 api.

Very odd. No fix yet!

Thanks
Graham

@a-kanaan
Copy link

same problem, any solution please?

@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
@anupamtdab
Copy link

Has anybody got the solution please?

@jorgearana
Copy link

jorgearana commented May 17, 2023

I have this message: "InvalidOperationException: Cannot use table 'AspNetRoles' for entity type 'AspNetRoles' since it is being used for entity type 'IdentityRole' and potentially other entity types, but there is no linking relationship. Add a foreign key to 'AspNetRoles' on the primary key properties and pointing to the primary key on another entity type mapped to 'AspNetRoles'."

I guess the solution is to forget .Net, there is no answers

@blessedneelesh
Copy link

blessedneelesh commented Mar 4, 2024

I am getting same error like @jorgearana

Any solution please?

Update: I deleted aspnet models generated by EF core tools and corrected context file.

@Firefly-night-SEA
Copy link

Firefly-night-SEA commented Feb 9, 2025

I have a fix that works for me

First inherits identity db like this
public partial class YourDbContext : IdentityDbContext<IdentityUser, IdentityRole, string>

Then delete the declaration of identity models inside of YourDbContext.cs, leaving only the fields you added.

Inside of OnModelCreating, change the identity tables into this:

    modelBuilder.Entity<IdentityRole>(entity =>
    {
        entity.HasIndex(e => e.NormalizedName, "RoleNameIndex")
            .IsUnique()
            .HasFilter("([NormalizedName] IS NOT NULL)");
    });

    modelBuilder.Entity<IdentityUser>(entity =>
    {
        entity.HasIndex(e => e.NormalizedUserName, "UserNameIndex")
            .IsUnique()
            .HasFilter("([NormalizedUserName] IS NOT NULL)");
    });

    modelBuilder.Entity<IdentityUserRole<string>>(entity =>
    {
        entity.HasKey(r => new { r.UserId, r.RoleId });
        entity.ToTable("AspNetUserRoles");
        entity.HasIndex(r => r.RoleId, "IX_AspNetUserRoles_RoleId");
    });

Basically, I changed AspNetUser and AspNetRole inside of OnModelCreating into IdentityUser, etc...

The final result should look something like this:

FunewsManagementContext.txt

@jorgearana
Copy link

jorgearana commented Feb 9, 2025 via email

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

No branches or pull requests

9 participants