EF Core剃须刀页面带有“包含”,“然后包含”。显示字符串而不是ID

时间:2019-11-03 08:36:47

标签: asp.net-core model-binding razor-pages

我有模型Client,Project,ClientComment,ProjectComment,ProjectStatus,RAGStatus和Resource。我正在显示按客户分组的与客户相关和与项目相关的评论的表。

我需要将相关的ProjectStatus,RAGStatus和Resource添加到我的表中,但是我无法在Include / ThenInclude查询中连接其他实体。

ProjectStatus,RAGStatus和Resource通过下拉菜单中的ViewData / VieWBag进行绑定,因此在ClientComment,Project和ProjectComment表中保存各自的ID(而不是实际的字符串)。

我试图为ProjectStatus,RAGStatus和Resource创建一个ViewModel,但是我无法找到Include / ThenInclude的正确组合来显示我的表上所需的所有信息。

客户端模型:

public class Client
{
    public int Id { get; set; }
    public string ClientName { get; set; }
    public bool IsActive { get; set; }
    public ICollection<ClientComment> ClientComments { get; set; }
    public ICollection<Project> Projects { get; set; }
}

项目模型:

public class Project
{
    public int Id { get; set; }
    public string ProjectName { get; set; }
    public int ClientId { get; set; }
    public Client Client { get; set; }

    public int ProjectStatusId { get; set; }
    public ProjectStatus ProjectStatus { get; set; }

    public int ResourceId { get; set; }
    public Resource Resource { get; set; }
    public bool IsArchived { get; set; }

    public ICollection<ProjectComment> ProjectComments { get; set; }
}

ClientComment模型:

public class ClientComment
{
    public int Id { get; set; }
    public int? ClientId { get; set; }
    public Client Client { get; set; }

    public int RAGStatusId { get; set; }
    public RAGStatus RAGStatus { get; set; }

    public string StatusComment { get; set; }
    public string EscalationComment { get; set; }
    public string GeneralComment { get; set; }
    public double? EOQ { get; set; }
    public DateTime LastUpdateDate { get; set; }
    public ClientComment ()
    {
        this.LastUpdateDate = DateTime.UtcNow;
    }

ProjectComment模型:

public class ProjectComment
{
    public int Id { get; set; }

    public int? ProjectId { get; set; }
    public Project Project { get; set; }

    public int RAGStatusId { get; set; }
    public RAGStatus RAGStatus { get; set; }

    public string StatusComment { get; set; }
    public string EscalationComment { get; set; }
    public string GeneralComment { get; set; }
    public double? EOQ { get; set; }
    public DateTime LastUpdateDate { get; set; }
    public ProjectComment ()
    {
        this.LastUpdateDate = DateTime.UtcNow;
    }

ProjectStatus模型:

public class ProjectStatus
{
    public int Id { get; set; }
    public string ProjectStatusName { get; set; }
    public bool IsActive { get; set; }
    public ICollection<Project> Projects { get; set; }
}

RAGStatus模型:

public class RAGStatus
{
    public int Id { get; set; }
    public string RAGStatusName { get; set; }
    public ICollection<ClientComment> ClientComments { get; set; }
    public ICollection<ProjectComment> ProjectComments { get; set; }
    public bool IsActive { get; set; }

}

资源模型:

public class Resource
{
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string LongName { get; set; }
    public bool IsActive { get; set; }

    public ICollection<Project> Projects { get; set; }
}

ClientVM模型:

public class ClientVM
{
public string ClientName { get; set; }
    public int ClientId { get; set; }
    public int ClientCommentId { get; set; }
    public string ClientStatusComment { get; set; }
    public int RAGStatusId { get; set; }
    public List<ProjectVM> Projectlist { get; set; }
    public List<RAGStatusVM> RAGStatuslist { get; set; }
}

ProjectVM模型:

public class ProjectVM
{
    public int ProjectId { get; set; }
    public string ProjectName { get; set; }
    public string ProjectStatusComment { get; set; }
    public int RAGStatusId { get; set; }
    public int ProjectStatusId { get; set; }
    public int ResourceId { get; set; }
}

PageModel:

    [BindProperty]
    public List<ClientVM> ClientVMList { get; set; }
    public List<ProjectVM> ProjectVMList { get; set; }
    public List<ResourceVM> ResourceVMList { get; set; }
    public List<ProjectStatusVM> ProjectstatusVMList { get; set; }
    public List<RAGStatusVM> RAGstatusVMList { get; set; }

    public async Task<IActionResult> OnGet()
    {
        var clientlist = await _context.Client
            .Include(c => c.ClientComments)
            .ThenInclude(cr => cr.RAGStatus)
            .Include(c => c.Projects)
                .ThenInclude(p => p.ProjectComments)
                .ThenInclude(pr => pr.RAGStatus).ToListAsync();

        ClientVMList = new List<ClientVM>();

        foreach (var item in clientlist)
        {
            ClientVM clientVM = new ClientVM()
            {
                Projectlist = new List<ProjectVM>()
            };
            clientVM.ClientId = item.Id;
            clientVM.ClientName = item.ClientName;
            if (item.ClientComments != null && item.ClientComments.Any())
            {
                clientVM.ClientStatusComment = item.ClientComments.OrderByDescending(cc => cc.LastUpdateDate).First().StatusComment;
            }
            else
            {
                clientVM.ClientStatusComment = "No StatusComment";
            }
            foreach (var projectItem in item.Projects)
            {

                ProjectVM projectVM = new ProjectVM
                {
                    ProjectId = projectItem.Id,
                    ProjectName = projectItem.ProjectName,
                    ProjectStatusId = projectItem.ProjectStatusId,
                    ResourceId = projectItem.ResourceId
                };
                if (projectItem.ProjectComments != null && projectItem.ProjectComments.Any())
                {
                    projectVM.ProjectStatusComment = projectItem.ProjectComments.OrderByDescending(pc => pc.LastUpdateDate).First().StatusComment;
                }
                else
                {
                    projectVM.ProjectStatusComment = "No StatusComment";
                }
                clientVM.Projectlist.Add(projectVM);
            }

            ClientVMList.Add(clientVM);
        }

        return Page();
    }
  

Client1 | ClientStatusComment | RAGStatus | ResourceName
  项目1 | ProjectStatusComment | RAGStatus |资源名称| ProjectStatusName
  Project2 | ProjectStatusComment | RAGStatus |资源名称| ProjectStatusName
  
  客户2 | ClientStatusComment | RAGStatus | ResourceName
  项目1 | ProjectStatusComment | RAGStatus |资源名称| ProjectStatusName
  Project2 | ProjectStatusComment | RAGStatus |资源名称| ProjectStatusName

0 个答案:

没有答案