MVC4 外键 属性 为空

MVC4 Foreign Key property is null

我有一个费用和类别 table。我已经为 table 播种了很少的条目,但是当从数据库中拉出时,类别 属性 为空。我希望它会自动填充类别 table 中的条目。我错过了什么?

型号:

public class Expense
{
    public int ExpenseId { get; set; }

    [DataType(DataType.Currency)]
    public decimal Amount { get; set; }

    [ForeignKey("CategoryId")]
    public Category Category { get; set; }

    [Required(ErrorMessage = "You must select some category!")]
    public int CategoryId { get; set; }
}
public class Category
{
    public int CategoryId { get; set; }
    public string Description { get; set; }
    public virtual ICollection<Expense> Expenses { get; set; }
}

查看:

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Amount)
        </td>
        <td>
            @item.CategoryId
        </td>
        <td>
            @item.Category.Description //NullReferenceException here
        </td>
    </tr>

控制器:

// GET: Expenses
    public ActionResult Index()
    {
        return View(db.Expenses.ToList());
    }

尝试将类别 属性 标记为 virtual。如果不是预先加载,导航属性需要是虚拟的以支持延迟加载。