c# - 用于映射用户集合的 EF Core Fluent API
问题描述
我有一个通知类
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 类中的 Notifications 属性相关的错误
无法确定“ICollection”类型的导航属性“ApplicationUser.Notifications”表示的关系。手动配置关系,或使用“[NotMapped]”属性或使用“OnModelCreating”中的“EntityTypeBuilder.Ignore”忽略此属性。
我相信这种关系应该是一对多的,即一个通知发送给许多应用程序用户,但是我在实体配置中的正常模式不起作用,我必须在其中一个类中遗漏一些东西。
我不确定如何使用流利的 API 将 Notifications 集合或外键关系映射到 UserProfile(我正在使用 IEntityTypeConfiguration 接口的 EntityConfiguration 类)
更新 Per Camilo 的答案,我更新了我的实体配置以包括 NavigationUser 表设置主键,如下所示
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.ForeignKey( name: "FK_AspNetUsers_Notifications_NotificationId",
column: x => x.NotificationId,
principalSchema: "MachineryCtx",
principalTable: "Notifications",
principalColumn: "NotificationId",
onDelete: ReferentialAction.Restrict);
和 NotificationUsers 表中的 ForeignKey 返回通知
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
推荐阅读
- java - MvcResult 的状态为 200,包括响应,但断言失败
- azure-devops - 推送 Azure Git 存储库时未调用生成触发器
- excel - 将对象添加到方法时,VBA 对象不支持此属性或方法
- c# - 泛型类的泛型参数的扩展方法
- javascript - 如何在 JS 中创建十六进制编码的 HmacSHA256 安全哈希?
- javascript - 按特定给定顺序对 JSON 数组进行排序
- python - 如何修复错误的 UTF-8 解码字符串?
- python - 如何在Python中修复“+:'int'和'list'不支持的操作数类型”
- notifications - 我创建了一个聊天应用程序,它每十秒钟从数据库中获取记录,但我想在任务栏显示通知
- c# - 通配符版本项目依赖