![](/img/trans.png)
[英]the correct way to map identity base class with fluent api in ef core?
[英]EF Core Fluent API to map collection of Users
我有一个通知课
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”忽略此属性。
我相信这种关系应该是一对多的,即一个Notification传递给许多ApplicationUser,但是我在Entity Configuration中的常规模式不起作用,我必须在其中一个类中缺少某些东西。
我不确定如何使用流畅的API将Notifications集合或外键关系映射到UserProfile(我正在通过IEntityTypeConfiguration接口使用EntityConfiguration类)
更新每个卡米洛的答案,我更新了我的实体配置,以包括设置主键的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
的复合主键
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.