【问题标题】:how to get back data from navigation properties from a many to many relationship如何从多对多关系的导航属性中获取数据
【发布时间】:2016-01-20 06:57:01
【问题描述】:

这是 Neighborhood 类,一个社区可以有很多关注者:

public class Neighbourhood
{
    public Neighbourhood()
    {
    this.Followerss = new HashSet<ApplicationUser>();     
    }
    [Key]
    public int Id { get; set; }
    public string NeighbourhoodName { get; set; }
  //Calculated property
  public virtual ICollection<ApplicationUser> Followerss { get; set; }
 }

这是我的 ApplicationUser 类,一个用户可以关注多个邻居:

 public class ApplicationUser : IdentityUser<int, CustomUserLogin, CustomUserRole,>
 { 
   public string Address { get; set; }
   public ApplicationUser()
   {
     this.Followings = new HashSet<Neighbourhood>(); 
    }
   public virtual ICollection<Neighbourhood> Followings { get; set; }  

现在,这两者之间存在多对多关系,因此,它在数据库中创建了一个名为 NeighbourhoodApplicationUsers 的新表,其中包含列(Neighbourhood_Id 和 ApplicationUser_Id)。

现在,我想要一个方法可以返回 Neighborhood 的追随者所以,我写了这个:

   public virtual IEnumerable<NeighbourhoodProfileModel> GetFollowers(int userId)
    {
        var profiles = _context.Neighbourhoods
            .SelectMany(u => u.Followerss)
            .Select(u => new NeighbourhoodProfileModel
            {
                NeighbourhoodId = u.,
                NeighbourhoodName = u.,
                followersCount = u.,
                IsFollowed = u.Followings.Any(user => user.Id == userId),
            }).ToList();
        return profiles;
    }

我不知道如何在这里建模这个匿名函数,我被困在这里,我想可能是我做错了。将 (.)dot 放在 u 之后,它应该返回 u.NeighbourhoodName 类似的东西,但它返回与用户相关的数据,而不是获取与邻域类相关的数据。

如果我注释掉 .SelectMany(u => u.followerss) 行,那么我会在匿名函数中获得所有必需的属性。

这是我的视图模型:

public class NeighbourhoodProfileModel
 {
    public int NeighbourhoodId { get; set; }
    public string NeighbourhoodName { get; set; }
    public string City { get; set; }
    public int postCount { get; set; }
    public int followersCount { get; set; }
    public int Followerss { get; set; }
    public bool IsFollowed { get; set; }
 }

请建议我如何修改 GetFollowers 方法以返回正确的数据。

【问题讨论】:

    标签: asp.net-mvc entity-framework linq-to-entities many-to-many anonymous-function


    【解决方案1】:

    首先它像预期的那样工作,SelectMany 从每个Neighbourhood 中获取Applicationusers 的集合,并将它们全部放在一个列表中,因此当您对它们进行投影时,您将获得ApplicationUser 的属性。

    其次,你想要做的事情是不可能的,你有一个多对多的关系,所以根据定义每个Applicationuser可以有多个Neighbourhoods,你怎么能指望找到一个 每个应用程序的邻居来获取 id 和 name 等?我认为您正在寻找的关系是一对多的,每个社区都可以有多个应用程序用户,但每个应用程序用户只有一个社区。当您将用户投射到 NeighbourhoodProfileModel 时,这将为您提供预期的行为。

    所以你的ApplicationUser 模型看起来像这样:

    public class ApplicationUser : IdentityUser<int, CustomUserLogin,CustomUserRole,>
    { 
      public string Address { get; set; }
    
      public virtual Neighbourhood NeighbourHood { get; set; }  
    }
    

    如果 Applicationuser 确实需要多个社区,您也可以选择使用 Select 而不是 SelectMany。这将创建每个 Neighbourhood 的 applicationuser 列表列表。那么投影应该是可以的。

    而且您不需要中间表。您还需要将Neighbourhood 的主键与ForeignKey 属性一起作为属性添加到ApplicationUser。 (除非您选择流畅的映射,否则会更好)。

    (顺便说一句,由于您投影到Neighbourhoodprofilemodel,所以您没有匿名函数,如果您有.Select(u =&gt; new {}),情况就是这样

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-05-19
      • 1970-01-01
      • 1970-01-01
      • 2022-12-20
      • 2014-01-13
      • 2019-05-23
      相关资源
      最近更新 更多