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

Implicit many to many relationship between IdentityRole and IdentityUser #23951

Closed
ArminShoeibi opened this issue Jan 24, 2021 · 1 comment
Closed
Labels
closed-no-further-action The issue is closed and no further action is planned. customer-reported

Comments

@ArminShoeibi
Copy link

ArminShoeibi commented Jan 24, 2021

Hello, in this video Mr. Shay Rojansky @roji introduced a way that gives us the ability to use Implicit Many to Many relationships besides the Join Entity,

here is the link to that video: https://youtu.be/BIImyq8qaD4?t=1038

now I want to make this happen for IdentityUser and IdentityRole because I want to query roles and users easily with my context class.

 public class ApplicationRole : IdentityRole<int>
    {
        public string DisplayName { get; set; }

        public ICollection<IdentityUserRole<int>> UserRoles { get; set; }

        public ICollection<ApplicationUser> Users { get; set; }
    }


 public class ApplicationUser : IdentityUser<int>
    {
        public string FirstName { get; set; }

        public string LastName { get; set; }

        public ICollection<ApplicationRole> Roles { get; set; } 

        public ICollection<IdentityUserRole<int>> UserRoles { get; set; }
    }

I Wrote the navigation properties like that video,
and this my config in Fluent API

     builder.Entity<ApplicationUser>()
                .HasMany(appUser => appUser.Roles)
                .WithMany(role => role.Users)
                .UsingEntity<IdentityUserRole<int>>
                (au => au.HasOne<ApplicationRole>().WithMany(role => role.UserRoles),
                au => au.HasOne<ApplicationUser>().WithMany(user => user.UserRoles));

but after creating a migration, the AspNetUserRoles has 4 properties.

    migrationBuilder.CreateTable(
                name: "UserRoles",
                columns: table => new
                {
                    UserId = table.Column<int>(type: "int", nullable: false),
                    RoleId = table.Column<int>(type: "int", nullable: false),
                    RolesId = table.Column<int>(type: "int", nullable: true),
                    UsersId = table.Column<int>(type: "int", nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_UserRoles", x => new { x.UserId, x.RoleId });
                    table.ForeignKey(
                        name: "FK_UserRoles_Roles_RoleId",
                        column: x => x.RoleId,
                        principalTable: "Roles",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                    table.ForeignKey(
                        name: "FK_UserRoles_Roles_RolesId",
                        column: x => x.RolesId,
                        principalTable: "Roles",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Restrict);
                    table.ForeignKey(
                        name: "FK_UserRoles_Users_UserId",
                        column: x => x.UserId,
                        principalTable: "Users",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                    table.ForeignKey(
                        name: "FK_UserRoles_Users_UsersId",
                        column: x => x.UsersId,
                        principalTable: "Users",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Restrict);
                });

How should I Write a correct configuration for achieving that feature in the video?

EF Core version:
Database provider: ( Microsoft.EntityFrameworkCore.SqlServer)
Target framework: (.NET 5.0.2)
Operating system:
IDE: ( Visual Studio 2019 16.8.4)

@ajcvickers
Copy link
Contributor

@ArminShoeibi This requires some additional configuration to match the existing join table. For example:

                    b.HasMany(e => e.Roles)
                        .WithMany(e => e.Users)
                        .UsingEntity<IdentityUserRole<int>>(
                            j => j.HasOne(e => e.Role).WithMany(e => e.UserRoles).HasForeignKey(e => e.RoleId),
                            j => j.HasOne(e => e.User).WithMany(e => e.UserRoles).HasForeignKey(e => e.RoleId));

@ajcvickers ajcvickers added the closed-no-further-action The issue is closed and no further action is planned. label Jan 25, 2021
ajcvickers added a commit that referenced this issue Jan 25, 2021
Checks that skip navigations can be layered over the existing model; see #23951.
ajcvickers added a commit that referenced this issue Jan 25, 2021
Checks that skip navigations can be layered over the existing model; see #23951.
@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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-no-further-action The issue is closed and no further action is planned. customer-reported
Projects
None yet
Development

No branches or pull requests

2 participants