将 IdentityUserClaim 导航添加到 IdentityUser<int>
Add IdentityUserClaim Navigation to IdentityUser<int>
我正在尝试将 IdentityUserClaim 导航添加到我的自定义 IdentityUser< int >,我尝试了很多方法但没有成功工作。
public class AppUser : IdentityUser<int>
{
[ForeignKey("UserId")]
public List<AppUserClaim> UserClaims { get; set; }
}
public class AppUserClaim : IdentityUserClaim<int>
{
}
public partial class ApplicationDbContext : IdentityDbContext<AppUser, AppRole, int, AppUserClaim, AppUserRole, AppUserLogin, AppRoleClaim, AppUserToken>
{
//DBSets
}
如果我 运行 没有迁移的应用程序(我的意思是 ef 迁移),那么我得到以下错误:
Microsoft.Data.SqlClient.SqlException (0x80131904): Invalid column name 'AppUserId'.
SELECT [a].[Id], [a].[AppUserId], [a].[ClaimType], [a].[ClaimValue],
[a].[UserId] FROM [AspNetUserClaims] AS [a] WHERE [a].[UserId] =
@__user_Id_0
而且是正确的,AspNetUserClaims table.
中没有 AppUserId 列
当我尝试添加迁移时,它会添加一个新列,我不会!
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<int>(
name: "AppUserId",
table: "AspNetUserClaims",
type: "int",
nullable: true);
migrationBuilder.CreateIndex(
name: "IX_AspNetUserClaims_AppUserId",
table: "AspNetUserClaims",
column: "AppUserId");
migrationBuilder.AddForeignKey(
name: "FK_AspNetUserClaims_AspNetUsers_AppUserId",
table: "AspNetUserClaims",
column: "AppUserId",
principalTable: "AspNetUsers",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
}
我怎么能告诉 ef core 我需要在现有 table 关系上导航?
好的,您已经继承了正确的基础 IdentityDbContext
和正确的泛型参数。
问题是基础 class 流畅地配置了没有导航属性的关系。由于fluent configuration的优先级高于data annotations,[ForeignKey]
属性被忽略,navigation属性被映射到一个separate关系与自己的FK.
因此,当您添加导航属性时,您还需要覆盖 OnModelCreating
并在 调用基本实现后重新配置关系,例如
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
builder.Entity<AppUser>()
.HasMany(e => e.UserClaims)
.WithOne()
.HasForeignKey(e => e.UserId);
}
我正在尝试将 IdentityUserClaim 导航添加到我的自定义 IdentityUser< int >,我尝试了很多方法但没有成功工作。
public class AppUser : IdentityUser<int>
{
[ForeignKey("UserId")]
public List<AppUserClaim> UserClaims { get; set; }
}
public class AppUserClaim : IdentityUserClaim<int>
{
}
public partial class ApplicationDbContext : IdentityDbContext<AppUser, AppRole, int, AppUserClaim, AppUserRole, AppUserLogin, AppRoleClaim, AppUserToken>
{
//DBSets
}
如果我 运行 没有迁移的应用程序(我的意思是 ef 迁移),那么我得到以下错误:
Microsoft.Data.SqlClient.SqlException (0x80131904): Invalid column name 'AppUserId'.
SELECT [a].[Id], [a].[AppUserId], [a].[ClaimType], [a].[ClaimValue], [a].[UserId] FROM [AspNetUserClaims] AS [a] WHERE [a].[UserId] = @__user_Id_0
而且是正确的,AspNetUserClaims table.
中没有 AppUserId 列当我尝试添加迁移时,它会添加一个新列,我不会!
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<int>(
name: "AppUserId",
table: "AspNetUserClaims",
type: "int",
nullable: true);
migrationBuilder.CreateIndex(
name: "IX_AspNetUserClaims_AppUserId",
table: "AspNetUserClaims",
column: "AppUserId");
migrationBuilder.AddForeignKey(
name: "FK_AspNetUserClaims_AspNetUsers_AppUserId",
table: "AspNetUserClaims",
column: "AppUserId",
principalTable: "AspNetUsers",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
}
我怎么能告诉 ef core 我需要在现有 table 关系上导航?
好的,您已经继承了正确的基础 IdentityDbContext
和正确的泛型参数。
问题是基础 class 流畅地配置了没有导航属性的关系。由于fluent configuration的优先级高于data annotations,[ForeignKey]
属性被忽略,navigation属性被映射到一个separate关系与自己的FK.
因此,当您添加导航属性时,您还需要覆盖 OnModelCreating
并在 调用基本实现后重新配置关系,例如
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
builder.Entity<AppUser>()
.HasMany(e => e.UserClaims)
.WithOne()
.HasForeignKey(e => e.UserId);
}