实体框架许多许多工程,但包括不 [英] Entity Framework Many to Many works but Include does not

查看:76
本文介绍了实体框架许多许多工程,但包括不的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我与这3张表有一个典型的多对多关系

I have a typical many-to-many relationship with these 3 tables

[Post] (
  [PostId] int, (PK)
  [Content] nvarchar(max)
   ...
)

[Tag] (
  [TagId] int, (PK)
  [Name] nvarchar
  ...
)

[TagPost] (
  [TagId] int, (PK, FK)
  [PostId] int (PK, FK)
)

而且,TagId和PostId是相应地在表上设置的PK和FK等等。然后我有这些类和映射在c#

And, TagId and PostId are the PK and FK set on the tables accordingly etc. Then I have these classes and mapping in c#

public class Post {
    public Post()
    {
        this.Tags = new HashSet<Tag>();
    }

    [Key]
    public int PostId { get; set; }
    ...

    public virtual ICollection<Tag> Tags { get; private set; }
}

public class Tag {
    public Tag()
    {
        this.Posts = new HashSet<Post>();
    }

    [Key]
    public int TagId { get; set; }
    ...

    public virtual ICollection<Post> Posts { get; private set; }
}

internal class MyDbContext : DbContext
{
    public DbSet<Post> Posts { get; set; }
    public DbSet<Tag> Tags { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        modelBuilder.Entity<Post>().ToTable("Post");
        modelBuilder.Entity<Tag>().ToTable("Tag");

        modelBuilder.Entity<Post>()
            .HasMany(x => x.Tags)
            .WithMany(x => x.Posts)
        .Map(x =>
        {
            x.ToTable("TagPost");
            x.MapLeftKey("PostId");
            x.MapRightKey("TagId");
        });
    }

然后我有这个代码来查询他们

Then I have this code to query them

var list = (from p in ctx.Posts.Include(p => p.Tags)
            from t in p.Tags
            where ... // some of my filter conditions
            select p).ToList();

此连接确实返回我正在寻找的帖子,但返回的帖子不是他们的关联标签即使我有包括在那里填写。有人可以帮忙指出我所缺少的东西,以便我可以随着帖子返回标签。

This join does return the posts I was looking for, however the returned posts don't their associated tags filled in even though I have the Include there. Could someone help point out what I'm missing so that I could have the tags also return with the posts?

非常感谢。

推荐答案

来自的双重是手动加入这导致 Include 被忽略,因为这里此处。对于其他LINQ方法, Include 也被忽略像分组和投影

The double from is a manual Join which causes the Include to be ignored as mentioned here and here. Include is also ignored for other LINQ methods like grouping and projections.

关系修正通常不适用于多对多关系,只适用于至少有一个单一引用的关系其中一个目的是一对多或一对一。如果您将帖子和相关的标签投影到另一种类型(匿名或命名)中,则数据将被正确加载,但因为关系是多对多的EF不会自动在内存中创建关系,以便 post.Tags 集合将保持为空。

Relationship fixup generally does not work for many-to-many relationships, only for relationships which have at least one single reference at one of the ends - one-to-many or one-to-one. If you project the Posts and related Tags into another type (anonymous or named) the data will be loaded correctly but because the relationship is many-to-many EF won't create the relationship in memory automatically so that the post.Tags collection will stay empty.

要获取 Include 工作,您必须从查询中删除中的第二个,然后应用其中子句直接连接到 Post 实体参数,例如:

To get the Include working you must remove the second from from your query and apply the where clause directly to the Post entity parameter, for example like so:

var list = (from p in ctx.Posts.Include(p => p.Tags)
            where p.Tags.Any(t => t.TagId == 1)
            select p).ToList();

指定了标签属性的过滤器在表达式中传递给 .Any 这是一个表达式,带有标签 t )作为参数。

The filter by a Tag property is specified in the expression passed into .Any which is an expression with a Tag (t) as parameter.

这篇关于实体框架许多许多工程,但包括不的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆