簡體   English   中英

EF7嵌套的“包含”未在Razor .net中顯示

[英]EF7 nested Include not showing in Razor .net

我有這樣的模型:

  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因此這意味着返回值為null,但是有用戶在帶有配置文件的數據透視表中。

型號:

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

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

如何使用剃須刀顯示嵌套數據?

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

當intellisense對於ThenInclude ,請不要ThenInclude ,只需鍵入它,它將進行編譯。

嘗試包括用戶參考

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM