E-COMMERCE

MAPPING A MANY-TO-MANY RELATIONSHIP WITH ENTITY FRAMEWORK CORE

NET CORE

We have come to the crux of our application, in fact the client asked us to make the MyCourse application because she wants to benefit financially from it. We can achieve this goal, for example, by having courses purchased and paid for online, a typical feature of e-commerce. Having completed the purchase we go on to ask the user to give a rating.

Relazione molti-a-molti

The relationship between users and courses is many-to-many, each course can enroll many users, and each user can enroll in many courses. To create the many-to-many relationship we need a third CourseStudents table, which relates the AspNetUsers and Courses tables.

Relazione utenti corsi
Implementazione
Mappare relazione molti a molti

Our client will also want to know this other information.

Colonne tabella di raccordo
Nome tabella

GETTING A USER TO ENROLL IN A COURSE

Let’s see what are the steps to purchase a course.

Iscrizione

The table involved is the one we created earlier.

Tabella Subscriptions

For the time being, we will take care of steps two and three by postponing the payment procedure until shortly. When we value the PaymentDate value in the controller, we do not choose DateTime.Now because it depends on the server settings, whereas DateTime.UtcNow does not.

Accesso
DateTime input model

I show you the code in the Controller.

Controller

We’ve written the code for enrollment, now we’re going to create a Policy that will allow us to hide the enroll button once enrollment has occurred and to regulate access to classes in a course. The Policy verifies that the user has enrolled in the course. Its name is CourseSubscriberRequirement.cs while its Handler is CourseSubscriberRequirementHandler.cs. Look at the implementation, by now you should have gained the necessary knowledge.

Policy Provider

LINK TO CODE ON GITHUB

GITHUB

Download the section code19 or the master branch or clone the GITHUB repository to have all the sections available in your favorite editor.