Foreign key relationship missing in ASP.NET MVC app using code-first approach with Entity Framework

jmk22

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.

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.

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.

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:

    <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.

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

Reza Aghaei

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);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Entity Framework 6 multiple table to one foreign key relationship code first

From Dev

Code first foreign key association MVC Entity

From Dev

Entity Framework Code First Foreign Key adding Index as well

From Dev

Accessing / Using a View Entity Framework 6 Code First approach

From Dev

How to correctly create a database using entity framework code first approach

From Dev

Entity Framework Code First Foreign Key issue

From Dev

Defining multiple Foreign Key for the Same table in Entity Framework Code First

From Dev

Entity Framework Code first adds unwanted foreign key column

From Dev

Entity Framework: Help Creating Database using Code first Approach

From Dev

Entity Framework 6 Code First Foreign Key Without Corresponding Properties

From Dev

Multiple Foreign Key for Same table in Entity Framework Code First

From Dev

Defining Self Referencing Foreign-Key-Relationship Using Entity Framework 7 Code First

From Dev

Entity framework 7 in ASP.NET MVC6 multiple foreign key to the same table

From Dev

Entity Framework Code First and Firebird - Foreign Key name issue

From Dev

Entity Framework - Code first - Data annotations - Unnecessary foreign key columns

From Dev

ASP.NET MVC How to access Entity Framework generated foreign key?

From Dev

Get foreign key value using Entity Framework code first

From Dev

Entity Framework Code First Foreign Key issue

From Dev

Entity Framework one to optional foreign key code first fluent mapping

From Dev

When using Entity Framework Code First Approach there is one additional table

From Dev

Accessing / Using a View Entity Framework 6 Code First approach

From Dev

Foreign Key in Code First Entity Framework

From Dev

Can I create a bidirectional foreign key relationship using Entity Framework Code First data annotations?

From Dev

Entity Framework: Help Creating Database using Code first Approach

From Dev

Entity framework code first cant create primary and foreign key relationship

From Dev

Login failed error when using Entity Framework code-first in ASP.NET MVC project

From Dev

Entity Framework: Foreign Key in code first

From Dev

Code first approach not creating foreign key

From Dev

ASP .NET - MVC 5 - Entity Framework - Code First - Forms Authentication

Related Related

  1. 1

    Entity Framework 6 multiple table to one foreign key relationship code first

  2. 2

    Code first foreign key association MVC Entity

  3. 3

    Entity Framework Code First Foreign Key adding Index as well

  4. 4

    Accessing / Using a View Entity Framework 6 Code First approach

  5. 5

    How to correctly create a database using entity framework code first approach

  6. 6

    Entity Framework Code First Foreign Key issue

  7. 7

    Defining multiple Foreign Key for the Same table in Entity Framework Code First

  8. 8

    Entity Framework Code first adds unwanted foreign key column

  9. 9

    Entity Framework: Help Creating Database using Code first Approach

  10. 10

    Entity Framework 6 Code First Foreign Key Without Corresponding Properties

  11. 11

    Multiple Foreign Key for Same table in Entity Framework Code First

  12. 12

    Defining Self Referencing Foreign-Key-Relationship Using Entity Framework 7 Code First

  13. 13

    Entity framework 7 in ASP.NET MVC6 multiple foreign key to the same table

  14. 14

    Entity Framework Code First and Firebird - Foreign Key name issue

  15. 15

    Entity Framework - Code first - Data annotations - Unnecessary foreign key columns

  16. 16

    ASP.NET MVC How to access Entity Framework generated foreign key?

  17. 17

    Get foreign key value using Entity Framework code first

  18. 18

    Entity Framework Code First Foreign Key issue

  19. 19

    Entity Framework one to optional foreign key code first fluent mapping

  20. 20

    When using Entity Framework Code First Approach there is one additional table

  21. 21

    Accessing / Using a View Entity Framework 6 Code First approach

  22. 22

    Foreign Key in Code First Entity Framework

  23. 23

    Can I create a bidirectional foreign key relationship using Entity Framework Code First data annotations?

  24. 24

    Entity Framework: Help Creating Database using Code first Approach

  25. 25

    Entity framework code first cant create primary and foreign key relationship

  26. 26

    Login failed error when using Entity Framework code-first in ASP.NET MVC project

  27. 27

    Entity Framework: Foreign Key in code first

  28. 28

    Code first approach not creating foreign key

  29. 29

    ASP .NET - MVC 5 - Entity Framework - Code First - Forms Authentication

HotTag

Archive