ASP中缺少外键关系。NET MVC应用程序使用代码优先的方法与实体框架

时间:2022-10-05 15:23:31

I am building an ASP.NET MVC 5 "To Do List" app, where users can add categories and add tasks to categories. I am using Entity Framework 7 with a sort of "Code First" approach, but using an existing database.

我正在构建一个ASP。NET MVC 5“To Do List”应用程序,用户可以添加类别并将任务添加到类别中。我使用的是实体框架7,使用的是一种“代码优先”方法,但使用的是现有的数据库。

I created a database in Microsoft SQL Server Management Studio with a Category and an Item table, and each Item has a CategoryId column that is a foreign key to the Category's Id column.

我在Microsoft SQL Server Management Studio中创建了一个数据库,其中包含一个类别和一个条目表,每个条目都有一个CategoryId列,这是这个类别的Id列的外键。

Then I created an empty MVC5 app, with a database connection to this database. I created an Item class and a Category class in the Models folder that correspond to the tables in the database.

然后我创建了一个空的MVC5应用程序,数据库连接到这个数据库。我在与数据库中的表对应的Models文件夹中创建了一个Item类和一个Category类。

Item class:

项目类:

namespace ToDoList.Models
{
    [Table("Item")]
    public partial class Item
    {
        public int ItemId { get; set; }
        public string Description { get; set; }

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

        public virtual Category Category { get; set; }
    }
}

Category class:

类别类:

namespace ToDoList.Models
{
    [Table("Category")]
    public partial class Category
    {
        public Category()
        {
            this.Items = new HashSet<Item>();
        }

        public int CategoryId { get; set; }
        public string Name { get; set; }
        public virtual ICollection<Item> Items { get; set; }
    }
}

I want the view for an Item to display its Category's Name property. The model for the view is a collection of all Items in the Item table (IEnumerable<ToDoList.Models.Item>). Say I want to print a list with each item followed by the name of the category related to it. Here's the code that I have:

我希望项的视图显示其类别的Name属性。视图的模型是项表中所有项的集合(IEnumerable - ToDoList.Models.Item>)。假设我要打印一个列表,每个条目后面跟着与之相关的类别的名称。这是我的代码:

    <ul>
    @foreach(var item in Model)
    {
        <li>@Html.DisplayFor(modelItem => item.Description) @Html.DisplayFor(modelItem => item.Category.Name)</li>;

    }
    </ul>

The Item shows up just fine, but the page doesn't display anything for the Category. This same code actually worked when I used an ADO.NET Entity Data Model to create classes, but I'm not able to create an Entity Data Model using ASP.NET 5, so I'm trying out this code-first approach.

条目显示得很好,但是页面没有显示类别的任何内容。当我使用ADO的时候,这段代码实际上是有效的。NET实体数据模型创建类,但我不能使用ASP创建实体数据模型。NET 5,我正在尝试这种代码优先的方法。

Any ideas on why the app isn't using the foreign key relationship or suggestions of what I can try?

对于为什么这个应用不使用外键关系或者我可以尝试的建议有什么想法吗?

1 个解决方案

#1


1  

You can use eager loading to include the Category in the query, using:

您可以使用即时加载将类别包含到查询中,使用:

var model = db.Items.Include("Category").ToList();

or

var model = db.Items.Include(x=>x.Category).ToList();

and then return the view: return View(data);

然后返回视图:返回视图(数据);

#1


1  

You can use eager loading to include the Category in the query, using:

您可以使用即时加载将类别包含到查询中,使用:

var model = db.Items.Include("Category").ToList();

or

var model = db.Items.Include(x=>x.Category).ToList();

and then return the view: return View(data);

然后返回视图:返回视图(数据);