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.

See original GitHub issue

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)

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:9 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
grahamorileycommented, Sep 6, 2022

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

Very odd. No fix yet!

Thanks Graham

2reactions
ErikEJcommented, Feb 2, 2022

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Cannot use table 'AspNetUsers' in schema '' for entity ...
Cannot use table 'AspNetUsers' in schema '' for entity 'AspNetUsers' since it is being used for another entity. There is no other dbContext ......
Read more >
Cannot use table 'AbpUsers' for entity type 'IdentityUser' since ...
ABP Framework version: v5.2.0 UI type: MVCr DB provider: EF Core Tiered (MVC) ... and potentially other entity types, but there is no...
Read more >
Access To User in AspNetUserRoles table - Microsoft Q&A
Hi,. in this code i want get user (ApplicationUser.cs) in AspNetUserRole tb , but just two field exists UserId,RoleId. 187226-untitled.png.
Read more >
The entity type IdentityUserLogin requires a primary key to be ...
Your browser can't play this video. Learn more.
Read more >
ASP.NET Core Identity 3.0 : Modifying the Identity Database
I simply have to issue the dotnet ef database update command and it will deploy the database to the connection string located in...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found