错误:EntityType未声明具有名称的导航属性

时间:2013-07-08 16:02:45

标签: c# asp.net-mvc

我正在尝试计算预算减去费用并在视图中显示。我有计算工作,但我坚持如何将结果合并到我的视图中。现在我收到错误:

  

EntityType TETSModel.Project未声明具有名称data的导航属性。

如果没有声明projects = from s in db.Projects.Include(p => data) select s;,这可以正常工作,但不会显示余额。

有人可以告诉我该怎么做吗?

来自控制器:

public ViewResult Index()
{
    var data = (from p in db.Projects
                join ex in db.ExpenseItems on p.ProjectKEY equals ex.ProjectKEY
                group ex by p into exgroup
                select new Classes.Budget
                {
                ProjectKEY = exgroup.Key.ProjectKEY,
                OverallBudgetMinusExpenses = exgroup.Key.OverallTechnologyBudget - exgroup.Sum(g => g.ExpenseAmount),
                CurrentBudgetMinusExpenses = exgroup.Key.CurrentPeriodBudget - exgroup.Sum(g => g.ExpenseAmount)
                    });
    var projects = from s in db.Projects.Include(p => p.ContractType).Include(p => p.Division)
               select s;
    projects = from s in db.Projects.Include(p => data)
               select s;
    return View(projects.ToList());
}

在视图中:

@model IEnumerable<TETS_DAL.Project>

@{
ViewBag.Title = "Projects";
}
<h2>All Projects</h2>
<p>
@Html.ActionLink("Create New Project", "Create")
</p>
@using (Html.BeginForm())
{
    <table style="padding: 5px 5px 5px 5px;">
    <tr>
        <th>
            <!--@Html.DisplayNameFor(model => model.ProjectKEY)-->
        </th>
        <th>Project<br />
            Number
        </th>
        <th>Office
        </th>
        <th>Project<br />
            Description
        </th>
        <th>Start<br />
            Date
        </th>
        <th>End<br />
            Date
        </th>
        <th>Current<br />
            Period<br />
            Start
        </th>
        <th>Current<br />
            Period<br />
            End
        </th>
        <th>Total<br />
            Budget
        </th>
        <th>Total<br />
            Budget<br />
            Balance
        </th>
        <th>Current<br />
            Budget
        </th>
        <th>Current<br />
            Budget<br />
            Balance
        </th>
        <th></th>
    </tr>

    @foreach (var item in Model)
    {
        <tr>
            <td>
                @Html.HiddenFor(modelItem => item.ProjectKEY)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.ProjectNumber)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Division.DivisionName)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.ProjectDescription)
            </td>
            <td>
                @String.Format("{0:d}", item.StartDate)
            </td>
            <td>
                @String.Format("{0:d}", item.OverallEndDate)
            </td>
            <td>
                @String.Format("{0:d}", item.CurrentPeriodStartDate)
            </td>
            <td>
                @String.Format("{0:d}", item.CurrentPeriodEndDate)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.OverallTechnologyBudget)
            </td>
            <td>
                @*  @Html.DisplayFor(modelItem => item.ExpenseItem.ExpenseAmount)*@
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.CurrentPeriodBudget)
            </td>
            <td>

            </td>
            <td>
            @Html.ActionLink("Edit", "Edit", new { id = item.ProjectKEY }) |
            @Html.ActionLink("Details", "Details", new { id = item.ProjectKEY }) |
            @Html.ActionLink("Expenses", "Index", "ExpenseItem", new { projectSearchString = item.ProjectNumber }, null)
            </td>
        </tr>
    }
</table>

}

预算类:

public class Budget
{
[Key]
public int ProjectKEY { get; set; }
public decimal? OverallBudgetMinusExpenses { get; set; }
public decimal? CurrentBudget { get; set; }
public decimal? CurrentBudgetMinusExpenses { get; set; }
public decimal? OverallBudget { get; set; }
}

项目模型:

namespace TETS_DAL
{
using System;
using System.Collections.Generic;

public partial class Project
{
    public Project()
    {
        this.ExpenseItems = new HashSet<ExpenseItem>();
    }

    public int ProjectKEY { get; set; }
    public Nullable<int> DivisionKEY { get; set; }
    public Nullable<int> ContractTypeKEY { get; set; }
    public string ProjectNumber { get; set; }
    public string ProjectDescription { get; set; }
    public Nullable<System.DateTime> StartDate { get; set; }
    public Nullable<System.DateTime> OverallEndDate { get; set; }
    public Nullable<System.DateTime> CurrentPeriodStartDate { get; set; }
    public Nullable<System.DateTime> CurrentPeriodEndDate { get; set; }
    public Nullable<decimal> OverallTechnologyBudget { get; set; }
    public Nullable<decimal> CurrentPeriodBudget { get; set; }
    public string Notes { get; set; }
    public Nullable<System.DateTime> CreateDate { get; set; }
    public Nullable<bool> Archive { get; set; }
    public Nullable<System.DateTime> LastUpdate { get; set; }
    public byte[] ts { get; set; }

    public virtual ContractType ContractType { get; set; }
    public virtual Division Division { get; set; }
    public virtual ICollection<ExpenseItem> ExpenseItems { get; set; }
}
}

1 个答案:

答案 0 :(得分:9)

你有以下几行:

var projects = from s in db.Projects.Include(p => p.ContractType).Include(p => p.Division)
           select s;
projects = from s in db.Projects.Include(p => data)
           select s;

为什么要两次分配变量projects

此外,正如您对问题的评论中所述,lambda表达式p => data表示,“对于每个p,返回其data属性。”但是,根据您收到的错误消息,Project类型没有名为data的属性。这就是你收到错误信息的原因。

但我想我明白你在做什么。您想在模型中同时拥有变量数据中的Project对象和Budget项吗?您可能需要定义另一个包含Project属性和Budget属性的类:

public ProjectViewModel
{
    Project Project { get;set;}
    Budget Budget { get;set;}
}

...并写一行或两行LINQ来创建ProjectViewModelprojects data个对象的集合,加入(我假设)ProjectKEY他们有共同的财产。