无法从 EF 对象获取智能感知属性

Cannot get intellisense properties from and EF object

我有一个建立了一对多关系的实体模型。

在我的数据库对象中,我获得了所有相关类别的所有相关项目。

在我的视图中,我试图显示每个类别和项目,但是,我无法从 Projects 实体中获取属性以显示在智能感知中。

我需要做什么才能发生这种情况?

类别实体

    namespace YeagerTechDB.Models
    {
        using System;
        using System.Collections.Generic;
        using System.ComponentModel.DataAnnotations;
        using System.Runtime.Serialization;

        [Serializable, DataContract(IsReference = true)]
        public partial class Category
        {
            public Category()
            {
                Projects = new HashSet<Project>();
            }

            [Key]
            [ScaffoldColumn(true)]
            [DataMember]
            public short CategoryID { get; set; }

            [Required]
            [StringLength(20)]
            [DataMember]
            public string CategoryDescription { get; set; }

            [DataMember]
            public virtual ICollection<Project> Projects { get; set; }
        }
    }

Project entity
--------------
namespace YeagerTechDB.Models
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.Runtime.Serialization;
    using YeagerTechDB.Enums;

    [Serializable, DataContract(IsReference = true)]
    public partial class Project
    {
        [Key]
        [ScaffoldColumn(false)]
        [Editable(false)]
        [DataMember]
        public short ProjectID { get; set; }

        [ScaffoldColumn(false)]
        [Editable(true)]
        [DataMember]
        public short CustomerID { get; set; }

        [Required(ErrorMessage = "Required!")]
        [StringLength(30)]
        [Display(Name = "ProjectName")]
        [DataMember]
        public string Name { get; set; }

        [Required]
        [DataType(DataType.MultilineText)]
        [Display(Name = "ProjectDescription")]
        [DataMember]
        public string Description { get; set; }

        [ScaffoldColumn(true)]
        [DataMember]
        public short CategoryID { get; set; }

        [Required(ErrorMessage = "Required!")]
        [DataMember]
        public CategoryTypeEnum CategoryType { get; set; }

        [ScaffoldColumn(true)]
        [DataMember]
        public short PriorityID { get; set; }

        [Required(ErrorMessage = "Required!")]
        [DataMember]
        public PriorityTypeEnum PriorityType { get; set; }

        [ScaffoldColumn(true)]
        [DataMember]
        public short StatusID { get; set; }

        [Required(ErrorMessage = "Required!")]
        [DataMember]
        public StatusTypeEnum StatusType { get; set; }

        [DataType(DataType.Currency)]
        [DataMember]
        public decimal? Quote { get; set; }

        [DataType(DataType.MultilineText)]
        [DataMember]
        public string Notes { get; set; }

        [DataType(DataType.DateTime)]
        [DataMember]
        public DateTime CreatedDate { get; set; }

        [DataType(DataType.DateTime)]
        [DataMember]
        public DateTime? UpdatedDate { get; set; }

        [DataMember]
        public virtual Category Category { get; set; }
        [DataMember]
        public virtual Customer Customer { get; set; }
        [DataMember]
        public virtual Priority Priority { get; set; }
        [DataMember]
        public virtual Status Status { get; set; }
        [DataMember]
        public virtual ICollection<TimeTracking> TimeTrackings { get; set; }
    }
}

数据库功能

public async Task<List<Category>> GetCategoryProjectsAsync()
{
    try
    {
        using (YeagerTech DbContext = new YeagerTech())
        {
            DbContext.Configuration.ProxyCreationEnabled = false;
            DbContext.Database.Connection.Open();

            var categories = await DbContext.Categories.Include("Projects").ToListAsync();

            return categories;
        }
    }
    catch (Exception)
    {
        throw;
    }
}

查看

@model IEnumerable<YeagerTechDB.Models.Category>

@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Index</h2>

<table class="table">
@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.CategoryID)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.CategoryDescription)
        </td>
        @foreach (var catProjItem in Model)
        {
            <td>
                @Html.DisplayFor(m => catProjItem.Projects.)
            </td>
        }
    </tr>
}

</table>

编辑视图

@model IEnumerable<YeagerTechDB.Models.Category>

@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Index</h2>

<table class="table">
@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.CategoryID)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.CategoryDescription)
        </td>
        @foreach (var catProjItem in item)
        {
            <td>
                @Html.DisplayFor(m => catProjItem.)
            </td>
        }
    </tr>
}

</table>

您在内部循环中遍历了错误的实体。而不是:

@foreach (var catProjItem in Model)

使用:

@foreach (var catProjItem in item.Projects)
{
    <td>
        @Html.DisplayFor(m => catProjItem.ProjectID)
    </td>
}

您的 catProjItem.Projects 是一个 ICollection<Project>,因此它没有 Project 属性,它有 ICollection 属性。