Adding Configuration to our Migration
In this lesson, we're going to add configuration to our migration
Get the project source code below, and follow along with the lesson material.
Download Project Source CodeTo set up the project on your local machine, please follow the directions provided in the README.md
file. If you run into any issues with running the project source code, then feel free to reach out to the author in the course's Discord channel.
This lesson preview is part of the The newline Guide to Fullstack ASP.NET Core and React course and can be unlocked immediately with a single-time purchase. Already have access to this course? Log in here.
Get unlimited access to The newline Guide to Fullstack ASP.NET Core and React with a single-time purchase.

[00:00 - 00:09] We just saw our migration file. This was created for us by entity framework just by looking at our entities and their relationships.
[00:10 - 00:20] What if we don't want some configurations and want to change them? For example, all the properties with type string are said to be true when null able.
[00:21 - 00:26] But we don't want that right? We got accept a cause without a title or without a description.
[00:27 - 00:36] This is what we are going to change now. We want to tell the migration configuration what we wanted to do rather than making default type migrations.
[00:37 - 00:50] What we need to do is create a new folder inside the infrastructure project and call it configuration. Inside this folder, we are going to write custom configurations inside a class.
[00:51 - 01:02] First create a new class and call it force configuration. We are going to implement entity type configuration interface.
[01:03 - 01:28] So let's use a colon followed by i entity type configuration which will be of type cause because we are writing the configuration for our cause. Let's import it using entity framework code using our quick fix.
[01:29 - 01:38] Now we just have to implement this interface. And of course we need to import cause entity as well.
[01:39 - 01:55] We can work with just the entity type builder. So we can get rid of everything else and import it again using Microsoft dot entity framework code or metadata dot builders.
[01:56 - 02:10] This gives us the method which helps us configure the entity which is cause in this case and we can use the builder to make the configuration. We can access the individual properties using builder.
[02:11 - 02:24] Let's get rid of this. We can access the individual properties using builder.property and provide specific expression that we want to implement.
[02:25 - 02:39] So in this case it can be for example p.id. Here p is our entity and p.id which is the property we want to configure.
[02:40 - 02:48] We can say for example it must be required. So we can say is required.
[02:49 - 03:01] 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.
[03:02 - 03:23] For example I want the title property to be non-nullable. So we can write builder.property, p goes to p.title property and we will make it as required as well.
[03:24 - 03:36] We can also chain it with other properties such as max length. So if you write has max length and we can give it a value of 150.
[03:37 - 03:44] I think 150 is enough for our title. Anything more than this will create an exception message.
[03:45 - 03:50] We can make all the string properties non-nullable. Let's copy it.
[03:51 - 04:07] I think we have 6 more properties so we can copy it 6 more times to. So we have the subtitle property p.subtitle.
[04:08 - 04:26] We have the language property so we can make it language description property instructor property. We also have the image property.
[04:27 - 04:41] Now if you remember we have a rating property which is a decimal. So we can write builder.property, p and the rating property.
[04:42 - 04:55] We can write has column type which will be decimal. It accepts two parameters precision and scale.
[04:56 - 05:10] Scale is the number of decimal places since we only want one and we want the precision to be 18 so we can write 18 and one and I will end it with a semicolor. We can also configure our relationship here.
[05:11 - 05:22] The one we created for category, learnings and requirements but then it will be an overkill but I will show you how to do it. Let's configure it for the category.
[05:23 - 05:40] I will go here and write builder.has1. Inside I can write p.category which means one course can have one category.
[05:41 - 06:07] Then we can say with many because one category will be associated with multiple courses. We can then mention the foreign key which is the category ID so we can chain it as foreign key which is our category ID and this is how we configure the relationships.
[06:08 - 06:15] Now I will comment it out but you can look it for reference. I will keep it in the code.
[06:16 - 06:36] And like we saw in the migration entity framework code has already done it for us automatically but this is the place where we can be extremely specific on what we want. Now what we want to do is tell our store context that we have custom configurations it has to use before making the migrations.
[06:37 - 06:49] What we can do is go to the store context file and I would like to tell you that it's not necessary to create new classes for making these configurations. You can do it inside the store context class as well.
[06:50 - 06:57] It's just personal preference. I wanted to keep this class clean so I created it in a different location.
[06:58 - 07:08] Writing it here is perfectly fine as well. Since we created it in a different location we just need to override a method which is called on model creating.
[07:09 - 07:39] So below the DB sets I will write protected void override and name of the method which is on model creating and inside I will pass the model builder and we will simply call it builder. By the way the on model creating is the method which is responsible for creating the migration.
[07:40 - 07:52] By overriding this method we are just telling it to look at our configurations. So we will write base which is the class we are deriving from which is called the DB context class.
[07:53 - 08:13] Paste dot on model creating and pass the builder. Now we will use the builder to apply configurations from assembly and then we can specify the assembly.
[08:14 - 08:34] It's not available so we will have to import it using system dot reflections and we want assembly dot get executing assembly. We see an error because override should be before writing void.
[08:35 - 08:43] Now we are ready to make the migration open the terminal. Make sure we are inside the root project.
[08:44 - 09:13] First of all let's remove the previous migration dot net if migrations remove starter project is API and project is infrastructure. Now let's add view migration dot net if migrations add and let's call it course configuration.
[09:14 - 09:31] Our starter project is API and infrastructure project. Let's open our migration files again.
[09:32 - 09:42] As we can see all the string fields are now non-nullable as we wanted. Also we see the decimal configuration that we applied in our rating property.
[09:43 - 09:55] We did not have to write any configuration for establishing one to many relationships because it was already done accurately by entity framework. Let's update this database in the next lesson.