Get up to 80 % extra points for free! More info:

Lesson 4 - E-shop in ASP.NET Core MVC - Relations and Repository

In the previous lesson, E-shop in ASP.NET Core MVC - Products and categories, we learned how to add additional entities to the data model, including DataAnnotation attributes. In today's ASP.NET Core tutorial, we'll add relations to these entities and update the database. Next, we'll have a look at one of the possible implementations of the Repository design pattern.

Relations

There's a M:N relation between the Product and the Category entities, because one category may contain multiple products, and one product may belong to multiple categories. We make this relationship happen by adding an association table with foreign key to both original tables. Unfortunately, EF Core (version 2.1) can't generate association tables for a Many-To-Many relationship yet, so we need to create it by ourselves.

We'll add a new CategoryProduct to the Models/ folder of the data project (we've chosen the name accordingly to the entities this table will bind together):

public class CategoryProduct
{
    public int CategoryId { get; set; }
    public virtual Category Category { get; set; }

    public int ProductId { get; set; }
    public virtual Product Product { get; set; }
}

Let's add a CategoryProduct collection to the Product and Category classes:

public virtual ICollection<CategoryProduct> CategoryProducts { get; set; }

In fact, we've now created two 1:N relationships in our classes. The first between the product and the items in the association table, the second one similarly for categories. Finally, we need to explicitly define


 

...End of the preview...
Continue further

You will gain knowledge worth hundreds of thousands for a few crowns

You've come here and that's great! We believe that the first lessons showed you something new and useful
Do you want to continue the course? Go to the premium section.

Buy this course

Buy all currently available lessons with exercise submitting and other features for just $89.10
Current account balance $0
By buying this package, you'll have access to all 34 articles (34 lessons) in this course.

Before buying this article, you have to buy the previous one

This article is licensed: Premium no-reselling II, by buying this article, you agree with the terms of use.

Commercial article (licence no-reselling)

Commercial article (licence no-reselling)

This article is based on many years of experience in the IT field, and describes how to develop a professional commercial product or parts, that can be directly used to generate profit or to get as a gate into the industry.

This knowledge is only for the members of our community who are working their way up to become IT professionals. Therefore, this knowledge is only available in exchange for credits. You can use the source code from this article for one commercial project. However, you will not be able to resell it. Simply put, you can't buy our article once and then sell our code multiple times. If you need to use this code a bit more extensively, we are ready to discuss Commercial licence options with you. You can find more info on this in the following article Licence.

Are you ready to become a professional? All you have to do is click here!.

Article description

Requested article covers this content:

Setting up the 1:N and M:N relations in Entity Framework Code First, inserting initialization and test data, the Repository design pattern implementation.

You gain credits by supporting our network. This is done by sending a helpful amount of money to support the site, or by creating content for the network.

Article has been written for you by Martin Petrovaj
Avatar
Activities