-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
Comments
Duplicate of #26820. Also, this can be done now with the EF Core Power Tools, |
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? |
@adil-portable use the advanced option to actually generate an entity |
Thank you very much @ErikEJ I am now able to generate AspNetUserRole object but I am still getting the same error: 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 |
I am getting the same error as you: adil-portable in a web .net 6 api. Very odd. No fix yet! Thanks |
same problem, any solution please? |
Has anybody got the solution please? |
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 |
I am getting same error like @jorgearana Any solution please? Update: I deleted aspnet models generated by EF core tools and corrected context file. |
I have a fix that works for me First inherits identity db like this 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:
Basically, I changed AspNetUser and AspNetRole inside of OnModelCreating into IdentityUser, etc... The final result should look something like this: |
Thanks, i will try it.
Regards
Ing. Jorge A. Arana Neyra
980 599 023
www.jorgearananeyra.com<http://www.jorgearananeyra.com>
"Bienvenido a la nube, programas y datos que pueden ser vistos por todos sin gastar en servidores "
…________________________________
De: Firefly-night-SEA ***@***.***>
Enviado: domingo, 9 de febrero de 2025 17:35
Para: dotnet/efcore ***@***.***>
Cc: Joge Arana ***@***.***>; Mention ***@***.***>
Asunto: Re: [dotnet/efcore] 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. (Issue #27251)
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...
—
Reply to this email directly, view it on GitHub<#27251 (comment)>, or unsubscribe<https://github.com/notifications/unsubscribe-auth/ABWB2QJZKAQVTDBYP6YDQA32O4ONDAVCNFSM6AAAAABWYUG56KVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDMNBWGE2DOMJUGM>.
You are receiving this because you were mentioned.Message ID: ***@***.***>
|
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
Now, when I am trying to use Microsoft.AspNetCore.Identity.EntityFrameworkCore method to get user details using FindByLoginAsync method. I get the error

My Program.cs is configured as:
I also tried by manually adding the table AspNetUserRole and adding the relationships as
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)
The text was updated successfully, but these errors were encountered: