无法从EF对象获取intellisense属性

时间:2015-01-22 16:26:03

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

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

在我的数据库对象中,我将所有相关项目都包含在所有相关类别中。

在我的视图中,我正在尝试显示每个类别和项目,但是,我无法将这些属性从Projects实体中提取出来进入intellisense。

为了让这种情况发生,我需要做些什么?

类别实体

    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>

1 个答案:

答案 0 :(得分:1)

你在内循环中循环错误的实体。而不是:

@foreach (var catProjItem in Model)

使用:

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

您的catProjItem.ProjectsICollection<Project>,因此它没有Project属性,但有ICollection个属性。