【问题标题】:What type of relationship is this and how should it be configured?这是什么类型的关系,应该如何配置?
【发布时间】:2021-05-10 07:36:09
【问题描述】:

我正在尝试使用身份创建一个讨论板,应用程序用户可以在其中创建、保存、隐藏和评论帖子。我能够在没有数据注释或覆盖 OnModelCreating 的情况下使帖子和评论工作,如下所示:

Post.cs:

public class Post
    {
        public int ID { get; set; }
        public string Title { get; set; }
        public string Content { get; set; }
        [DataType(DataType.DateTime)]
        public DateTime CreationDate { get; set; }
        public ApplicationUser OriginalPoster { get; set; }
        public int Upvotes { get; set; }
        public int Downvotes { get; set; }
        public int VoteScore { get; set; }
        public ICollection<Comment> Comments { get; set; }
    }

评论.cs:

public class Comment
    {
        public int ID { get; set; }
        public string Content { get; set; }
        public ApplicationUser Commenter { get; set; }
        [DataType(DataType.DateTime)]
        public DateTime CreationDate { get; set; }
        public int Upvotes { get; set; }
        public int Downvotes { get; set; }
        public int VoteScore { get; set; }
    }

ApplicationDbContext.cs:

public class ApplicationDbContext : IdentityDbContext
    {
        public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
            : base(options)
        {
        }

        public DbSet<Post> Posts { get; set; }
        public DbSet<Comment> Comments { get; set; }
    }

但是当我扩展 IdentityUser 以添加我自己的自定义字段时:

public class ApplicationUser : IdentityUser
    {
        public ICollection<Post> CreatedPosts { get; set; }
        public ICollection<Post> SavedPosts { get; set; }
        public ICollection<Post> HiddenPosts { get; set; }
    }

添加迁移返回错误:

"无法确定导航所代表的关系 'ICollection' 类型的'ApplicationUser.CreatedPosts'。任何一个 手动配置关系,或使用 '[NotMapped]' 属性或使用 'EntityTypeBuilder.Ignore' 'OnModelCreating'。”

为什么 EF Core 能够确定帖子与其评论之间的关系,但不能确定 ApplicationUser 与其创建/保存/隐藏的帖子之间的关系?我知道我必须通过使用数据注释或覆盖 OnModelCreating 来指定关系,但我不确定如何去做。任何数量的帮助都将非常感激。

【问题讨论】:

    标签: c# entity-framework .net-core entity-framework-core asp.net-identity


    【解决方案1】:

    原因是因为您有多个集合属性引用同一个模型Post。这种情况你需要专门告诉 EF Core CreatedPostsHiddenPostsSavedPosts 分别从 Post 引用哪些外部属性。假设您只有一个名为 OriginalPosterApplicationUser 外部属性,这是不可能的,因为没有其他属性 HiddenPostsSavedPosts 可以引用。您只能通过像这样配置它来引用它。

    builder.Entity<ApplicationUser>().HasMany(s => s.CreatedPosts)
        .WithOne(f => f.OriginalPoster);
    

    现在,其他两个(HiddenPostsSavedPosts)引用了哪些属性?我希望你能在这里看到问题。

    但假设您在 Post 模型中定义了另一种类型的海报,如下所示。

    public ApplicationUser HiddenPoster {get;set;} 
    

    您将其所属的集合也用于引用它。

    builder.Entity<ApplicationUser>().HasMany(s => s.HiddenPosts)
        .WithOne(f => f.HiddenPoster);
    

    但你没有,所以这种方法行不通,因为它只是你在Post 中的一种海报。我建议你重新定义你的模型,在Post 中有一个枚举值CreatedHiddenSaved

    public enum PostStatus
    {
        Created,
        Hidden,
        Saved
    }
    

    然后像这样在Post模型中定义状态。

    public PostStatus Status {get;set;}
    

    这样在您的ApplicationUser 中,您不必定义多个集合,您只有Posts

    public class ApplicationUser : IdentityUser
    { 
        public ICollection<Post> Posts {get;set;}
    }
    

    然后您可以使用来自PostStatus 枚举属性过滤创建、隐藏或保存的帖子。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-21
      • 1970-01-01
      相关资源
      最近更新 更多