EF Core 多个一对多关系
EF Core Multiple one to many relationship
我正在构建简单的应用程序,使用 entity framework 来创建应该看起来像的关系。
public class Post
{
public long Id { get; set; }
public string Title;
public ICollection<Comment> Comments { get; set; } = new List<Comment>();
public ApplicationUser Owner { get; set; }
public ICollection<Tag> Tags { get; set; } = new List<Tag>();
public int LikesCount { get; set; }
}
public class ApplicationUser: IdentityUser
{
public ICollection<Post> UserPost { get; set; } = new List<Post>();
public ICollection<Post> LikedPosts { get; set; } = new List<Post>();
}
public class Comment : Auditable
{
public long Id { get; set; }
public string Content { get; set; }
}
public class Tag
{
public long Id { get; set; }
public string TagString { get; set; }
}
评论和标签是空的classes,只有索引。
如何在用户 User class 和 Post 之间创建正确的关系?
这就是我流利的结果api:
builder.Entity<Post>(post =>
{
post.HasKey(p => p.Id);
post.HasOne(p => p.Owner)
.WithMany(u => u.LikedPosts)
.HasForeignKey(p => p.Id)
.OnDelete(DeleteBehavior.Cascade);
post.HasOne(p => p.Owner)
.WithMany(u => u.UserPost)
.HasForeignKey(p => p.Id)
.OnDelete(DeleteBehavior.Cascade);
});
这让我出错,关系已经存在。
我希望 sql 创建单独的表格来对喜欢和拥有的帖子进行分组。
感谢您的帮助。
我假设两个 ApplictionUser 可能喜欢同一个 Post,所以像这样:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Post>()
.HasOne(p => p.Owner)
.WithMany(u => u.UserPost)
.OnDelete(DeleteBehavior.Restrict);
modelBuilder.Entity<ApplicationUser>()
.HasMany(u => u.LikedPosts)
.WithMany(p => p.Likers);
base.OnModelCreating(modelBuilder);
}
我正在构建简单的应用程序,使用 entity framework 来创建应该看起来像的关系。
public class Post
{
public long Id { get; set; }
public string Title;
public ICollection<Comment> Comments { get; set; } = new List<Comment>();
public ApplicationUser Owner { get; set; }
public ICollection<Tag> Tags { get; set; } = new List<Tag>();
public int LikesCount { get; set; }
}
public class ApplicationUser: IdentityUser
{
public ICollection<Post> UserPost { get; set; } = new List<Post>();
public ICollection<Post> LikedPosts { get; set; } = new List<Post>();
}
public class Comment : Auditable
{
public long Id { get; set; }
public string Content { get; set; }
}
public class Tag
{
public long Id { get; set; }
public string TagString { get; set; }
}
评论和标签是空的classes,只有索引。
如何在用户 User class 和 Post 之间创建正确的关系?
这就是我流利的结果api:
builder.Entity<Post>(post =>
{
post.HasKey(p => p.Id);
post.HasOne(p => p.Owner)
.WithMany(u => u.LikedPosts)
.HasForeignKey(p => p.Id)
.OnDelete(DeleteBehavior.Cascade);
post.HasOne(p => p.Owner)
.WithMany(u => u.UserPost)
.HasForeignKey(p => p.Id)
.OnDelete(DeleteBehavior.Cascade);
});
这让我出错,关系已经存在。 我希望 sql 创建单独的表格来对喜欢和拥有的帖子进行分组。
感谢您的帮助。
我假设两个 ApplictionUser 可能喜欢同一个 Post,所以像这样:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Post>()
.HasOne(p => p.Owner)
.WithMany(u => u.UserPost)
.OnDelete(DeleteBehavior.Restrict);
modelBuilder.Entity<ApplicationUser>()
.HasMany(u => u.LikedPosts)
.WithMany(p => p.Likers);
base.OnModelCreating(modelBuilder);
}