C# 这是什么类型的关系,应该如何配置?
我正在尝试使用Identity创建一个讨论板,ApplicationUsers可以在其中创建、保存、隐藏和评论帖子。我能够在没有数据注释或重写ModelCreating的情况下获得帖子和评论,如下所示: 邮政编码:C# 这是什么类型的关系,应该如何配置?,c#,entity-framework,.net-core,entity-framework-core,asp.net-identity,C#,Entity Framework,.net Core,Entity Framework Core,Asp.net Identity,我正在尝试使用Identity创建一个讨论板,ApplicationUsers可以在其中创建、保存、隐藏和评论帖子。我能够在没有数据注释或重写ModelCreating的情况下获得帖子和评论,如下所示: 邮政编码: public class Post { public int ID { get; set; } public string Title { get; set; } public string Content { get; set
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; }
}
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; }
}
public类ApplicationDbContext:IdentityDbContext
{
公共应用程序DBContext(DbContextOptions选项)
:基本(选项)
{
}
公共DbSet Posts{get;set;}
公共DbSet注释{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; }
}
公共类应用程序用户:IdentityUser
{
公共ICollection CreatedPosts{get;set;}
公共ICollection SavedPosts{get;set;}
公共ICollection HiddenPosts{get;set;}
}
添加迁移返回,错误为:
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与其创建/保存/隐藏的帖子之间的关系?我知道我必须通过使用数据注释或重写ModelCreating来指定关系,但我不确定如何进行此操作。非常感谢您提供的任何帮助。原因是您有多个集合属性引用同一模型,
Post
。在这种情况下,您需要特别告诉EF Core哪些外部属性是CreatedPosts
、HiddenPosts
和SavedPosts
从Post
引用的。如果您只有一个名为OriginalPoster
的ApplicationUser
外部属性,这将是不可能的,因为没有其他属性HiddenPosts
和SavedPosts
将引用。您只能通过如下配置来引用一个
builder.Entity<ApplicationUser>().HasMany(s => s.CreatedPosts)
.WithOne(f => f.OriginalPoster);
public ApplicationUser HiddenPoster {get;set;}
public PostStatus Status {get;set;}
您使它所属的集合也引用它
builder.Entity<ApplicationUser>().HasMany(s => s.HiddenPosts)
.WithOne(f => f.HiddenPoster);
然后像这样在Post
模型中定义状态
builder.Entity<ApplicationUser>().HasMany(s => s.CreatedPosts)
.WithOne(f => f.OriginalPoster);
public ApplicationUser HiddenPoster {get;set;}
public PostStatus Status {get;set;}
这样,在应用程序用户
中,您不必定义多个集合,您只需要帖子
public class ApplicationUser : IdentityUser
{
public ICollection<Post> Posts {get;set;}
}
公共类应用程序用户:IdentityUser
{
公共ICollection Posts{get;set;}
}
然后,您可以使用post
中的Status
enum属性筛选创建、隐藏或保存的帖子