本文介绍了EF7嵌套在剃刀.NET包括不显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的模型是这样的:


  1. Goup.cs

  2. GroupUser(透视表)

  3. ApplicationUser(用户) - > 4.档案

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

 私人的IEnumerable< GroupUser> GetUsers(INT的groupId)
    {
        IEnumerable的< GroupUser>模型= NULL;        如果(的groupId == 0)
        {
            模型= _kletsContext.GroupUser.OrderByDescending(O => o.GroupId).AsEnumerable();
        }
        其他
        {
            模型= _kletsContext.GroupUser.Where(G => g.GroupId ==的groupId).INCLUDE(P => p.User.Profile).OrderByDescending(O = GT; o.GroupId).AsEnumerable();
        }        回归模型;
    }

这工作,如果我只是想显示的用户ID,......(所以在数据透视表中的数据)这code:

  @model IEnumerable的< App.Models.GroupUser>
@if(型号= NULL&放大器;!&安培; Model.Count()0)
{
@foreach(以型号VAR用户)
{
@ user.UserId< / H>
}
}

但由于某些原因,我不能显示包括表中的数据?
通常你会做这样的事情:// @ user.User.Profile.XXXX但我得到的错误: System.NullReferenceException:对象不设置到对象的实例
因此,这将意味着返回的是空的,但也有在枢轴表用户提供信息。

该机型:

Group.cs:

 命名空间App.Models
{
公共类组:项目
{
    公开组():基地()
    {    }    [键]
    公众的Int16标识{搞定;组; }
    公共字符串名称{;组; }
    公共字符串图片{搞定;组; }    / *外键* /
    公众可空<&Int16的GT; RegionId {搞定;组; }    公共虚拟地区地区{搞定;组; }
    公共虚拟的ICollection<让>让{搞定;组; }
    公共虚拟的ICollection< GroupUser>用户{搞定;组; }
}
}

ApplicationUser:

 命名空间App.Models.Identity
{
公共类ApplicationUser:IdentityUser
{
    公共字符串描述{搞定;组; }
    公众的DateTime CreatedAt {搞定;组; }
    公众可空<&日期时间GT; UpdatedAt {搞定;组; }
    公众可空<&日期时间GT; DeletedAt {搞定;组; }    / *虚拟或导航属性* /
    公共虚拟档案资料{搞定;组; }
    公共虚拟的ICollection< GroupUser>论坛{搞定;组; }
    公共虚拟的ICollection<让>让{搞定;组; }
    公共虚拟的ICollection<类别及GT;分类{搞定;组; }
    公共虚拟的ICollection<地区GT;地区{搞定;组; }
    公共虚拟的ICollection<状态>状态{搞定;组; }
    公共虚拟的ICollection<标记和GT;标签{搞定;组; }
}
}

GroupUser:

 命名空间App.Models
{
公共类GroupUser
{
    公共GroupUser()
    {    }    公众可空<&Int16的GT; GroupId的{搞定;组; }
    公共字符串userid {搞定;组; }
    公共虚拟组组{搞定;组; }
    公共虚拟ApplicationUser用户{搞定;组; }
}
}

Profile.cs:

 命名空间App.Models
{
公共类简介:项目
{
    公开资料():基地()
    {    }    [键]
    公共字符串userid {搞定;组; }
    公共字符串名字{获得;组; }
    公共字符串姓{搞定;组; }
    公共字符串电子邮件{获得;组; }
    公共字符串性别{搞定;组; }
    公众的Int16年龄{搞定;组; }
    公共字符串城{搞定;组; }
    公共字符串图像{搞定;组; }
    公众的Int16学分{搞定;组; }
    公众的Int16邮政code {搞定;组; }
}
}

我怎样才能用剃刀显示嵌套数据?


解决方案

 模型= _kletsContext.GroupUser.Where(G => g.GroupId ==的groupId)
    .INCLUDE(区=> gu.User)
    .ThenInclude(U => u.Profile)
    .OrderByDescending(O => o.GroupId)
    .AsEnumerable();

不要被吓坏了,当智能感知没有为 ThenInclude ,只需键入它,它就会编译。

工作

I have my models like this:

  1. Goup.cs
  2. GroupUser (pivot table)
  3. ApplicationUser (User) -> 4. Profile

And now I want to show the data in Profile on a details page when the User belongs to the group. I'm doing this like this:

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;
    }

This works, if I just want to display the UserId, ... (so the data in the Pivot table) with this code:

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

But for some reason I can't display the data in the Included tables?Normally you would do something like this: @user.User.Profile.XXXX but then I get the error: System.NullReferenceException: Object reference not set to an instance of an objectSo this would mean the return is null, but there are users in the pivot table with a profile.

The models:

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; }
}
}

ApplicationUser:

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; }
}
}

GroupUser:

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; }
}
}

How can i display the nested data with razor?

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

Don't get freaked out when intellisense doesn't work for the ThenInclude, just type it, it will compile.

这篇关于EF7嵌套在剃刀.NET包括不显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 15:19