我想包含用户的角色列表。
var users = Users
.Include(u => u.UserRoles)
.ToList()
我遵循了许多建议来解决问题,但没有一个对我有用:
- EF Core 2.0 Identity - Adding navigation properties
- http://kontext.tech/docs/DotNetEssential/p/migrating-from-aspnet-core-1x-to-aspnet-core-20
- https://github.com/aspnet/EntityFrameworkCore/issues/9503
所以很可能其他人也处于同样的情况。
启动.cs
services.AddIdentity<User, Role>()
.AddEntityFrameworkStores<ApiDbContext>()
.AddDefaultTokenProviders()
;//.AddUserStore<UserStore<User, Role, ApiDbContext, Guid>>()//, IdentityUserClaim<Guid>, UserRole, IdentityUserLogin<Guid>, IdentityUserToken<Guid>, IdentityRoleClaim<Guid>>>()
//.AddRoleStore<RoleStore<Role, ApiDbContext, Guid>>();//, UserRole, IdentityRoleClaim < Guid >>> ();
[注意]:在评论中,有许多不同的尝试使迁移工作但没有成功
角色.cs
public class Role : IdentityRole<Guid>
{
public virtual ICollection<UserRole> UserRoles { get; } = new List<UserRole>();
}
用户.cs
public class User : IdentityUser<Guid>, IEntity
{
public virtual ICollection<UserRole> UserRoles { get; } = new List<UserRole>();
}
UserRole.cs
public class UserRole : IdentityUserRole<Guid>
{
public virtual User User { get; }
public virtual Role Role { get; }
}
ApiDbContext.cs
public class ApiDbContext : IdentityDbContext<User, Role, Guid, IdentityUserClaim<Guid>, UserRole, IdentityUserLogin<Guid>, IdentityRoleClaim<Guid>, IdentityUserToken<Guid>>
{
protected override void OnModelCreating(ModelBuilder builder)
{
builder.Entity<User>()
.HasMany(e => e.UserRoles)
.WithOne()
.HasForeignKey(e => e.UserId)
.IsRequired()
.OnDelete(DeleteBehavior.Cascade);
builder.Entity<UserRole>()
.HasOne(e => e.User)
.WithMany(e => e.UserRoles)
.HasForeignKey(e => e.UserId);
builder.Entity<UserRole>()
.HasOne(e => e.Role)
.WithMany(e => e.UserRoles)
.HasForeignKey(e => e.RoleId);
base.OnModelCreating(builder);
}
每次我运行“Add-migration”时,无论遵循什么建议,我总是会:
migrationBuilder.CreateTable(
name: "AspNetUserRoles",
columns: table => new
{
UserId = table.Column<Guid>(nullable: false),
RoleId = table.Column<Guid>(nullable: false),
RoleId1 = table.Column<Guid>(nullable: true),
UserId1 = table.Column<Guid>(nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_AspNetUserRoles", x => new { x.UserId, x.RoleId });
table.ForeignKey(
name: "FK_AspNetUserRoles_AspNetRoles_RoleId",
column: x => x.RoleId,
principalTable: "AspNetRoles",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "FK_AspNetUserRoles_AspNetRoles_RoleId1",
column: x => x.RoleId1,
principalTable: "AspNetRoles",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
table.ForeignKey(
name: "FK_AspNetUserRoles_AspNetUsers_UserId",
column: x => x.UserId,
principalTable: "AspNetUsers",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "FK_AspNetUserRoles_AspNetUsers_UserId1",
column: x => x.UserId1,
principalTable: "AspNetUsers",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
});
Role1 和 User1 不应该在那里。
我正在使用:
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="2.0.2" />
有人知道如何解决这个问题吗?
最佳答案
尝试将 setter 添加到 UserRole 的属性中。
public class UserRole : IdentityUserRole<Guid>
{
public virtual User User { get; set; }
public virtual Role Role { get; set; }
}
如果您需要只读它,您可以添加一个私有(private)构造函数。
public class UserRole : IdentityUserRole<Guid>
{
private User _user;
private Role _role;
private UserRole()
{
}
public UserRole(User user, Role role)
{
_user = user;
_role = role;
}
public virtual User User => _user;
public virtual Role Role => _role;
}
但是您需要在 dbcontext 中配置私有(private)字段。
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<UserRole>().Property(u => u.User).HasField("_user");
modelBuilder.Entity<UserRole>().Property(u => u.Role).HasField("_role");
}
关于c# - EF Core 为用户添加角色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49471126/