EF查询项目多对多

时间:2014-11-18 07:38:48

标签: linq entity-framework entity-framework-5

我有一个像这样的自动生成多对多的关系

modelBuilder.Entity<Shop>()
            .HasMany(x => x.ShopCategories)
            .WithMany(x => x.Shops)
        .Map(x =>
        {
            x.ToTable("ShopCategories");
            x.MapLeftKey("ShopId");
            x.MapRightKey("CategoryId");
        });

然后我试图通过包含相关实体来查询商店

        var result = from s in _ctx.Shops
                     from su in s.ShopUsers
                     where su.UserId == userId
                     orderby s.Name
                     select new { Shop = s, Suburb = s.Suburb, ShopUser = s.ShopUsers, ShopCategories = s.ShopCategories };

在匿名类型的结果商店对象中,它填充了Suburb,ShopUsers 但ShopCategories为空。

为什么EF没有为该特定属性填充商店对象?

**编辑

链接表是自动生成的,它不是我拥有的类。

public class Shop
{
    ...
    public ICollection<ShopCategory> ShopCategories { get; set; }

}

public class ShopCategory
{
    public List<Shop> Shops{ get; set; }
}

0 个答案:

没有答案