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

Add many-to-many mapping for Identity Users/Roles relationship #23973

Merged
merged 1 commit into from
Jan 25, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,42 @@ await ExecuteWithStrategyInTransactionAsync(
});
}

[ConditionalFact]
public async Task Can_lazy_load_User_navigations_many_to_many()
{
var userId = "";

await ExecuteWithStrategyInTransactionAsync(
async context =>
{
var user = new CustomUserString { NormalizedUserName = "wendy" };
await CreateUser(context, user);
userId = user.Id;
},
async context =>
{
var user = await context.Users.SingleAsync(u => u.Id == userId);

Assert.Equal(2, user.Roles.Count);
});
}

[ConditionalFact]
public async Task Can_lazy_load_Role_navigations_many_to_many()
{
await ExecuteWithStrategyInTransactionAsync(
async context =>
{
await CreateUser(context, new CustomUserString { NormalizedUserName = "wendy" });
},
async context =>
{
var role = await context.Roles.OrderBy(e => e.NormalizedName).FirstAsync();

Assert.Equal(1, role.Users.Count);
});
}

[ConditionalFact]
public async Task Can_lazy_load_UserRole_navigations()
{
Expand Down Expand Up @@ -183,6 +219,10 @@ protected override List<EntityTypeMapping> ExpectedMappings
"Navigation: CustomRoleString.RoleClaims (ICollection<CustomRoleClaimString>) Collection ToDependent CustomRoleClaimString Inverse: Role PropertyAccessMode.Field",
"Navigation: CustomRoleString.UserRoles (ICollection<CustomUserRoleString>) Collection ToDependent CustomUserRoleString Inverse: Role PropertyAccessMode.Field",
},
SkipNavigations =
{
"SkipNavigation: CustomRoleString.Users (ICollection<CustomUserString>) CollectionCustomUserString Inverse: Roles PropertyAccessMode.Field"
}
},
new EntityTypeMapping()
{
Expand Down Expand Up @@ -248,7 +288,7 @@ protected override List<EntityTypeMapping> ExpectedMappings
{
"Navigation: CustomUserRoleString.Role (CustomRoleString) ToPrincipal CustomRoleString Inverse: UserRoles PropertyAccessMode.Field",
"Navigation: CustomUserRoleString.User (CustomUserString) ToPrincipal CustomUserString Inverse: UserRoles PropertyAccessMode.Field",
},
}
},
new EntityTypeMapping()
{
Expand Down Expand Up @@ -285,6 +325,10 @@ protected override List<EntityTypeMapping> ExpectedMappings
"Navigation: CustomUserString.Tokens (ICollection<CustomUserTokenString>) Collection ToDependent CustomUserTokenString Inverse: User PropertyAccessMode.Field",
"Navigation: CustomUserString.UserRoles (ICollection<CustomUserRoleString>) Collection ToDependent CustomUserRoleString Inverse: User PropertyAccessMode.Field",
},
SkipNavigations =
{
"SkipNavigation: CustomUserString.Roles (ICollection<CustomRoleString>) CollectionCustomRoleString Inverse: Users PropertyAccessMode.Field"
}
},
new EntityTypeMapping()
{
Expand Down Expand Up @@ -328,6 +372,12 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
modelBuilder.Entity<CustomUserString>(
b =>
{
b.HasMany(e => e.Roles)
.WithMany(e => e.Users)
.UsingEntity<CustomUserRoleString>(
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));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hey guys, can someone tell me please if both has fk should be using roleid? seems to me the second should be userid? am i missing something

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, it should be UserId. It's actually overridden a few lines below

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks I thought so just wanted to confirm


b.HasMany(e => e.Claims).WithOne(e => e.User).HasForeignKey(uc => uc.UserId).IsRequired();
b.HasMany(e => e.Logins).WithOne(e => e.User).HasForeignKey(ul => ul.UserId).IsRequired();
b.HasMany(e => e.Tokens).WithOne(e => e.User).HasForeignKey(ut => ut.UserId).IsRequired();
Expand Down Expand Up @@ -390,6 +440,8 @@ public CustomUserString()

public string CustomTag { get; set; }

public virtual ICollection<CustomRoleString> Roles { get; set; }

public virtual ICollection<CustomUserClaimString> Claims { get; set; }
public virtual ICollection<CustomUserLoginString> Logins { get; set; }
public virtual ICollection<CustomUserTokenString> Tokens { get; set; }
Expand All @@ -403,6 +455,8 @@ public CustomRoleString()
Id = Guid.NewGuid().ToString();
}

public virtual ICollection<CustomUserString> Users { get; set; }

public virtual ICollection<CustomUserRoleString> UserRoles { get; set; }
public virtual ICollection<CustomRoleClaimString> RoleClaims { get; set; }
}
Expand Down