【问题标题】:EF7 nested Include not showing in Razor .netEF7 嵌套包含未显示在 Razor .net 中
【发布时间】:2016-04-01 03:56:57
【问题描述】:

我的模型是这样的:

  1. Goup.cs
  2. GroupUser(数据透视表)
  3. ApplicationUser(用户)-> 4. Profile

现在我想在用户属于该组时在详细信息页面上显示配置文件中的数据。我这样做是这样的:

private IEnumerable<GroupUser> GetUsers(int groupId)
    {
        IEnumerable<GroupUser> model = null;

        if(groupId == 0)
        {
            model = _kletsContext.GroupUser.OrderByDescending(o => o.GroupId).AsEnumerable();
        }
        else
        {
            model = _kletsContext.GroupUser.Where(g => g.GroupId == groupId).Include(p => p.User.Profile).OrderByDescending(o => o.GroupId).AsEnumerable();
        }

        return model;
    }

如果我只想使用以下代码显示 UserId,...(因此数据透视表中的数据),这可行:

@model IEnumerable<App.Models.GroupUser>
@if(Model != null && Model.Count() > 0)
{   
@foreach(var user in Model)
{
@user.UserId</h2>
}
}

但由于某种原因,我无法显示包含表中的数据? 通常你会做这样的事情:@user.User.Profile.XXXX 但后来我得到错误:System.NullReferenceException: Object reference not set to an instance of an object 所以这意味着返回为空,但数据透视表中有用户具有个人资料。

模型:

Group.cs:

namespace App.Models
{
public class Group : Item
{
    public Group() : base()
    {

    }

    [Key]
    public Int16 Id { get; set; } 
    public string Name { get; set; }
    public string Images { get; set; }

    /* Foreign Keys */
    public Nullable<Int16> RegionId { get; set; }

    public virtual Region Region { get; set; }
    public virtual ICollection<Lets> Lets { get; set; }
    public virtual ICollection<GroupUser> Users { get; set; }
}
}

应用用户:

namespace App.Models.Identity
{
public class ApplicationUser : IdentityUser
{   
    public string Description { get; set; }
    public DateTime CreatedAt { get; set; }
    public Nullable<DateTime> UpdatedAt { get; set; }
    public Nullable<DateTime> DeletedAt { get; set; } 

    /* Virtual or Navigation Properties */
    public virtual Profile Profile { get; set; }
    public virtual ICollection<GroupUser> Groups { get; set; }    
    public virtual ICollection<Lets> Lets { get; set; }  
    public virtual ICollection<Category> Categories { get; set; } 
    public virtual ICollection<Region> Regions { get; set; }    
    public virtual ICollection<Status> Status { get; set; }   
    public virtual ICollection<Tag> Tags { get; set; }   
}
}

组用户:

namespace App.Models
{
public class GroupUser
{
    public GroupUser()
    {

    }

    public Nullable<Int16> GroupId { get; set; }
    public string UserId { get; set; }
    public virtual Group Group { get; set; }
    public virtual ApplicationUser User { get; set; }
}
}

Profile.cs:

namespace App.Models
{
public class Profile : Item
{
    public Profile() : base()
    {

    }

    [Key]
    public string UserId { get; set; } 
    public string FirstName { get; set; }
    public string SurName { get; set; }
    public string Email { get; set; }
    public string Gender { get; set; }
    public Int16 Age { get; set; }
    public string City { get; set; }
    public string Image { get; set; }
    public Int16 Credits { get; set; }
    public Int16 Postalcode { get; set; }
}
}

如何使用 razor 显示嵌套数据?

【问题讨论】:

  • 你会分享模型吗?我认为那里有问题。
  • 我已经更新了问题:)。
  • 使用ToList() 代替.AsEnumerable()
  • 私有 IEnumerable GetUsers(int groupId) { IEnumerable model = null; if(groupId == 0) { model = _kletsContext.GroupUser.Include(g => g.Group).OrderByDescending(o => o.GroupId).ToList(); } else { 模型 = _kletsContext.GroupUser.Where(g => g.GroupId == groupId).Include(p => p.User.Profile).ToList(); } 返回模型;所以我结束了这个。但这不起作用。我仍然遇到同样的错误。

标签: c# asp.net-mvc entity-framework razor entity-framework-core


【解决方案1】:
model = _kletsContext.GroupUser.Where(g => g.GroupId == groupId)
    .Include(gu => gu.User)
    .ThenInclude(u => u.Profile)
    .OrderByDescending(o => o.GroupId)
    .AsEnumerable();

当智能感知不适用于 ThenInclude 时不要惊慌,只需输入它,它就会编译。

【讨论】:

  • 这很完美! Intellisense 并没有吓坏 :)!。对于未来的读者,然后我就像这样在剃须刀中获取数据:@model IEnumerable&lt;App.Models.GroupUser&gt; @if(Model != null &amp;&amp; Model.Count() &gt; 0) { @foreach(var user in Model) { @user.User.Profile.FirstName } }
【解决方案2】:

尝试包含用户参考

model = _kletsContext.GroupUser.Where(g => g.GroupId == groupId).Include(p => p.User).Include(p => p.User.Profile) .OrderByDescending(o => o.GroupId).AsEnumerable();

【讨论】:

  • 这也不起作用。我假设我也已经链接了用户(我的智能感知是这样说的):)。
  • 在 public virtual ApplicationUser User { get; 行之前添加注释 [ForeignKey("UserId")]放; } 在 Groupuser 中。
猜你喜欢
  • 2021-11-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-29
相关资源
最近更新 更多