【问题标题】:EF Core Fluent API to map collection of Users用于映射用户集合的 EF Core Fluent API
【发布时间】:2019-05-23 16:15:49
【问题描述】:

我有一个通知类

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”忽略此属性。

我相信这种关系应该是一对多的,即一个通知发送给许多应用程序用户,但我在实体中的正常模式 配置不起作用,我一定是在其中一个类中遗漏了一些东西。

我不确定如何使用 fluent API 将 Notifications 集合或外键关系映射到 UserProfile(我正在使用 IEntityTypeConfiguration 接口使用 EntityConfiguration 类)

更新 根据 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 表中的外键返回通知

table.ForeignKey(                       name: "FK_NotificationUser_Notifications_NotificationId",
      column: x => x.NotificationId,
      principalSchema: "MachineryCtx",
      principalTable: "Notifications",
      principalColumn: "NotificationId",
      onDelete: ReferentialAction.Cascade);

【问题讨论】:

    标签: c# entity-framework-core ef-fluent-api


    【解决方案1】:

    您正在尝试将多对多关系建模为一对多。

    你应该有这样的东西:

    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 的复合主键

    【讨论】:

    • ...并且 NotificationUser 成为数据库中的连接表?还是未映射?
    • @dinotom 您需要数据库中的连接表。这是映射多对多的唯一合乎逻辑的方式
    • 它不会那样迁移......“实体类型'NotificationUser'需要定义一个主键。” .
    • @dinotom Agh,迁移,无用的技术...更新后 :)
    • ...节日快乐,因为您今天花时间回复。查看我的更新,创建了两个外键,但它看起来不正确。但见鬼,我知道什么,我有问题。如果是对的,我会接受你的回答。我只是不明白这如何让我收集到任何一个。
    猜你喜欢
    • 2014-07-22
    • 1970-01-01
    • 2017-01-19
    • 2014-08-24
    • 1970-01-01
    • 1970-01-01
    • 2019-11-28
    • 2011-02-10
    • 1970-01-01
    相关资源
    最近更新 更多