EF Core Fluent API 映射用户集合
EF Core Fluent API to map collection of Users
我有一个通知class
public class Notification
{
public int NotificationId { get; set; }
public string NotificationMessage { get; set; }
public DateTime NotificationSentOn { get; set; }
//TODO: not sure how to map this in fluent api
// a Notification can go to many users
public ICollection<ApplicationUser> ReceivingUsers { get; set; }
}
和 ApplicationUser 的扩展
public class ApplicationUser : IdentityUser
{
public ApplicationUser()
{
IsAuthor = false;
}
public string Firstname { get; set; }
public string Lastname { get; set; }
public Gender Gender { get; set; }
public DateTime? DateOfBirth { get; set; }
public DateTime RegisteredDate { get; set; }
public bool IsAuthor { get; set; }
// foreign key to UserProfile using the string ID from ApplicationUser
public UserProfile MemberProfile { get; set; }
// collection of notifications for this user
public ICollection<Notification> Notifications { get; set; }
}
这是与 ApplicationUser class
中的通知 属性 有关的错误
Unable to determine the relationship represented by navigation property 'ApplicationUser.Notifications' of type 'ICollection'. Either manually configure the relationship, or ignore this property using the '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'.
我认为这种关系应该是一对多的,即一个通知发送给多个 ApplicationUser,但我在实体中的正常模式
配置不起作用,我一定是在 classes 之一中遗漏了一些东西。
我不确定如何使用流利的方式将通知集合或外键关系映射到 UserProfile API(我正在使用 EntityConfiguration classes 使用 IEntityTypeConfiguration 接口)
更新
根据 Camilo 的回答,我更新了我的实体配置以包括 NavigationUser table 设置主键如下
public class NotificationUserEntityConfiguration : IEntityTypeConfiguration<NotificationUser>
{
public void Configure(EntityTypeBuilder<NotificationUser> builder)
{
builder.HasKey(u => new { u.ApplicationUserId, u.NotificationId })
.HasName("PK_NotificationUser");
builder.Property(u => u.NotificationId)
.ValueGeneratedNever()
.IsRequired();
builder.Property(u => u.ApplicationUserId)
.ValueGeneratedNever()
.IsRequired();
}
}
这从数据库创建脚本返回了以下内容
它在 ApplicationUser 中创建了一个 ForeignKey table
table.ForeignKey( name: "FK_AspNetUsers_Notifications_NotificationId",
column: x => x.NotificationId,
principalSchema: "MachineryCtx",
principalTable: "Notifications",
principalColumn: "NotificationId",
onDelete: ReferentialAction.Restrict);
和 NotificationUsers 中的外键 table 返回通知
table.ForeignKey( name: "FK_NotificationUser_Notifications_NotificationId",
column: x => x.NotificationId,
principalSchema: "MachineryCtx",
principalTable: "Notifications",
principalColumn: "NotificationId",
onDelete: ReferentialAction.Cascade);
您正在尝试将多对多关系建模为一对多关系。
你应该有这样的东西:
public class ApplicationUser
{
...
public ICollection<NotificationUser> Notifications { get; set; }
}
public class Notification
{
...
public ICollection<NotificationUser> Users { get; set; }
}
public class NotificationUser
{
public int ApplicationUserId { get; set; }
public int NotificationId { get; set; }
public ApplicationUser User { get; set; }
public Notification Notification { get; set; }
}
也就是说:
- 一个用户可以有多个通知
- 一个通知可以有多个用户
您可以拥有 IDENTITY
主键或具有 ApplicationUserId,NotificationId
的复合主键
我有一个通知class
public class Notification
{
public int NotificationId { get; set; }
public string NotificationMessage { get; set; }
public DateTime NotificationSentOn { get; set; }
//TODO: not sure how to map this in fluent api
// a Notification can go to many users
public ICollection<ApplicationUser> ReceivingUsers { get; set; }
}
和 ApplicationUser 的扩展
public class ApplicationUser : IdentityUser
{
public ApplicationUser()
{
IsAuthor = false;
}
public string Firstname { get; set; }
public string Lastname { get; set; }
public Gender Gender { get; set; }
public DateTime? DateOfBirth { get; set; }
public DateTime RegisteredDate { get; set; }
public bool IsAuthor { get; set; }
// foreign key to UserProfile using the string ID from ApplicationUser
public UserProfile MemberProfile { get; set; }
// collection of notifications for this user
public ICollection<Notification> Notifications { get; set; }
}
这是与 ApplicationUser class
中的通知 属性 有关的错误Unable to determine the relationship represented by navigation property 'ApplicationUser.Notifications' of type 'ICollection'. Either manually configure the relationship, or ignore this property using the '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'.
我认为这种关系应该是一对多的,即一个通知发送给多个 ApplicationUser,但我在实体中的正常模式 配置不起作用,我一定是在 classes 之一中遗漏了一些东西。
我不确定如何使用流利的方式将通知集合或外键关系映射到 UserProfile API(我正在使用 EntityConfiguration classes 使用 IEntityTypeConfiguration 接口)
更新 根据 Camilo 的回答,我更新了我的实体配置以包括 NavigationUser table 设置主键如下
public class NotificationUserEntityConfiguration : IEntityTypeConfiguration<NotificationUser>
{
public void Configure(EntityTypeBuilder<NotificationUser> builder)
{
builder.HasKey(u => new { u.ApplicationUserId, u.NotificationId })
.HasName("PK_NotificationUser");
builder.Property(u => u.NotificationId)
.ValueGeneratedNever()
.IsRequired();
builder.Property(u => u.ApplicationUserId)
.ValueGeneratedNever()
.IsRequired();
}
}
这从数据库创建脚本返回了以下内容
它在 ApplicationUser 中创建了一个 ForeignKey table
table.ForeignKey( name: "FK_AspNetUsers_Notifications_NotificationId",
column: x => x.NotificationId,
principalSchema: "MachineryCtx",
principalTable: "Notifications",
principalColumn: "NotificationId",
onDelete: ReferentialAction.Restrict);
和 NotificationUsers 中的外键 table 返回通知
table.ForeignKey( name: "FK_NotificationUser_Notifications_NotificationId",
column: x => x.NotificationId,
principalSchema: "MachineryCtx",
principalTable: "Notifications",
principalColumn: "NotificationId",
onDelete: ReferentialAction.Cascade);
您正在尝试将多对多关系建模为一对多关系。
你应该有这样的东西:
public class ApplicationUser
{
...
public ICollection<NotificationUser> Notifications { get; set; }
}
public class Notification
{
...
public ICollection<NotificationUser> Users { get; set; }
}
public class NotificationUser
{
public int ApplicationUserId { get; set; }
public int NotificationId { get; set; }
public ApplicationUser User { get; set; }
public Notification Notification { get; set; }
}
也就是说:
- 一个用户可以有多个通知
- 一个通知可以有多个用户
您可以拥有 IDENTITY
主键或具有 ApplicationUserId,NotificationId