.net core 多对多关系只包含一个实体
Dotnet Core 2.2、EntityFrameworkCore 2.2.3 在实体“Post”和“Category”之间的多对多关系中,链接实体“PostCategory”返回“Post”对象,但对于“Category”对象,仅返回Id,而不返回对象本身 迁移和数据库更新工作正常,所有三个表都已创建 对于关系本身,我尝试使用EF“auto magic”和ApplicationDbContext中onmodel创建中关系的显式定义 模型 后模型.net core 多对多关系只包含一个实体,.net-core,entity-framework-core,.net Core,Entity Framework Core,Dotnet Core 2.2、EntityFrameworkCore 2.2.3 在实体“Post”和“Category”之间的多对多关系中,链接实体“PostCategory”返回“Post”对象,但对于“Category”对象,仅返回Id,而不返回对象本身 迁移和数据库更新工作正常,所有三个表都已创建 对于关系本身,我尝试使用EF“auto magic”和ApplicationDbContext中onmodel创建中关系的显式定义 模型 后模型 public class Post {
public class Post
{
public int Id { get; set; }
public string Slug { get; set; }
public string Title { get; set; }
public string Abstract { get; set; }
public string Content { get; set; }
public string Author { get; set; }
public DateTime Created { get; set; }
public ICollection<PostCategory> PostCategories { get; set; }
}
ApplicationDbContext中的数据库集
public DbSet<Post> BlogPosts { get; set; }
public DbSet<Category> BlogCategories { get; set; }
public DbSet<PostCategory> PostCategories { get; set; }
结果显示在屏幕截图中
在ApplicationDbContext中,我尝试了两个版本:
第1版:
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
builder.Entity<PostCategory>()
.HasKey(x => new { x.PostId, x.CategoryId });
}
public DbSet<Post> BlogPosts { get; set; }
public DbSet<Category> BlogCategories { get; set; }
public DbSet<PostCategory> PostCategories { get; set; }
模型创建时受保护的覆盖无效(ModelBuilder)
{
基于模型创建(生成器);
builder.Entity()
.HasKey(x=>new{x.PostId,x.CategoryId});
}
公共DbSet BlogPosts{get;set;}
公共数据库集BlogCategories{get;set;}
公共数据库集后分类{get;set;}
第2版:
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
builder.Entity<PostCategory>()
.HasKey(x => new { x.PostId, x.CategoryId });
builder.Entity<PostCategory>()
.HasOne(pt => pt.Post)
.WithMany(p => p.PostCategories)
.HasForeignKey(pt => pt.PostId);
builder.Entity<PostCategory>()
.HasOne(pt => pt.Category)
.WithMany(t => t.PostCategories)
.HasForeignKey(pt => pt.CategoryId); ;
}
public DbSet<Post> BlogPosts { get; set; }
public DbSet<Category> BlogCategories { get; set; }
public DbSet<PostCategory> PostCategories { get; set; }
模型创建时受保护的覆盖无效(ModelBuilder)
{
基于模型创建(生成器);
builder.Entity()
.HasKey(x=>new{x.PostId,x.CategoryId});
builder.Entity()
.HasOne(pt=>pt.Post)
.WithMany(p=>p.PostCategories)
.HasForeignKey(pt=>pt.posted);
builder.Entity()
.HasOne(pt=>pt.Category)
.有许多(t=>t.后分类)
.HasForeignKey(pt=>pt.CategoryId);
}
公共DbSet BlogPosts{get;set;}
公共数据库集BlogCategories{get;set;}
公共数据库集后分类{get;set;}
版本迁移和更新都没有错误,结果相同
我很感激你的帮助
致意
编辑:
我以前尝试过“ThenInclude”,但显然我的Visual Studio自动完成有一个问题:
如果我忽略了自动完成,那么它就工作了,谢谢 要在多个级别加载相关数据,您必须使用
。然后包括,如下所示:
public IEnumerable<Post> GetAll()
{
var posts = _context.BlogPosts
.Include(x => x.PostCategories)
.ThenInclude(pc => pc.Category);
return posts;
}
public IEnumerable GetAll()
{
var posts=\u context.BlogPosts
.包括(x=>x.后分类)
.然后包括(pc=>pc.Category);
返回岗位;
}
这里是更多的细节:你缺少。然后包括(x=>x.Category)
你(1)回答重复的(2)没有任何解释(3)有冗余的代码。包括(x=>x.PostCategories)。然后包括(pc=>pc.Post)
@IvanStoev 1)在您将其标记为重复之前,我已经开始回答了2)添加了解释3)删除了多余的代码。4) 谢谢:)似乎是一个自动完成问题,请参阅原始帖子。@TanvirArjel(1)您需要在回答之前搜索重复项(2)很好(3)很好(4)欢迎:)我尝试了“ThenInclude”,但如果我忽略并编写另一个实体,那么它就可以正常工作。谢谢
public IActionResult Index()
{
var blogPosts2 = _blogService.GetAll();
...
}
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
builder.Entity<PostCategory>()
.HasKey(x => new { x.PostId, x.CategoryId });
}
public DbSet<Post> BlogPosts { get; set; }
public DbSet<Category> BlogCategories { get; set; }
public DbSet<PostCategory> PostCategories { get; set; }
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
builder.Entity<PostCategory>()
.HasKey(x => new { x.PostId, x.CategoryId });
builder.Entity<PostCategory>()
.HasOne(pt => pt.Post)
.WithMany(p => p.PostCategories)
.HasForeignKey(pt => pt.PostId);
builder.Entity<PostCategory>()
.HasOne(pt => pt.Category)
.WithMany(t => t.PostCategories)
.HasForeignKey(pt => pt.CategoryId); ;
}
public DbSet<Post> BlogPosts { get; set; }
public DbSet<Category> BlogCategories { get; set; }
public DbSet<PostCategory> PostCategories { get; set; }
public IEnumerable<Post> GetAll()
{
var posts = _context.BlogPosts
.Include(x => x.PostCategories)
.ThenInclude(pc => pc.Category);
return posts;
}