This video is available to students only

Adding Configuration to our Migration

In this lesson, we're going to add configuration to our migration

Configuring the migration#

We just saw our migration file. This was created for us by Entity Framework just by looking at our entities and their relationships. What if we don't want some configurations and want to change them? For example, all the properties with type string are set to be true when nullable, but we don't want that right? We can't accept a course without a title or without a description. This is what we're going to change now. We want to tell the migration configuration what we want it to do rather than making default type migrations.

What we need to do is create a new folder inside Infrastructure project and we can call it Configuration. Inside this folder, we are going to write custom configurations inside a class. Let's create a new class and call it CourseConfiguration. We are going to implement EntityTtypeConfigurationInterface, so we'll use a colon followed by IEntityTypeConfiguration which will be of type Course. Let's import it using Entity Framework Core using quick fix. Now we just need to implement this interface and of course import the Course Entity as well.

We can work with just the EntityTypeBuilder so we can get rid of everything else and import it again using Microsoft.EntityFrameworkCore.Metadata.Builders.

This gives us the method which helps us configure the Entity which is Course in this case and we can use builder to make the configuration. We can access the individual properties using builder.Property and provide the specific expression that we want to implement. For example, we can write P which is our entity and p.Id which is the property we want to configure. We can for example, say it must be required, so we can write isRequired. This is just to demonstrate you how it works because the Id being the primary key is always required in any table.

More importantly, we want to configure the string properties in our Entity. For example, I want the Title property to be non-nullable, so we can write builder.Property, p.Title to be required as well. We can also chain it with other properties such as MaxLength which can be 150. I think 150 is enough for our title. Anything more than this will create an exception message. We can make all the string properties non-nullable. Let's copy it six more times for the other properties; we have SubTitle property, language property, level property, description property, instructor property and the image property. For Instructor, we can put a limit to 50. Level can be 20. We have a rating property which is a decimal, so we can write HasColumnType which will be decimal. It accepts two parameters, precision and scale. Scale is the number of decimal places. Since we want only one, we can write 18 and 1.

We can also configure our relationship here; the one we created for Category, Learnings and Requirements. But then that will be an overkill. However, I'll show you how to do it. Let's configure it for Category, builder.HasOne which means one Course can have one category, then we can say with many because one category will be associated with multiple courses. We can then mention the foreign key which is CategoryId. Now I will comment it out, you can look at it for reference. Like we saw in the migration, Entity Framework core has already done it for us automatically. But this is the place where we can be extremely specific on what we want.

Infrastructure/Config/CourseConfiguration

Start a new discussion. All notification go to the author.