Create migration
Last updated
Last updated
Use the below CLI command to create a migration which will intern create the database, the tables and the required columns in it as per the model we created
We should now be able to see a migrations folder and a few files in it as shown
If we open the first file inside the migrations folder, we should see generated code which looks like this
First, the migration command searched our project for a file or a class which derives from DbContext
once it found that it looked into the contents, especially a property or a set of properties of type DbSet<T>
It understood that they are our model files, and it needs to create database tables for the models by further looking at the properties of the model, which is Student
in our case
It further created the C# code required to programmatically create them
It has also done the mapping of C sharp data types to their respective SQL Server column types
Entity framework has also identified the Id property of student class to be the primary key for the to-be -created Student
table. This is based on conventions which we can always override as and when required!
At this stage we have no database or tables created we can now do it by running the following cli command
Once the above command runs successfully, we see that the database, tables and the columns are created accordingly
If you do not see SQL server object explorer panel in your Visual Studio, you can open it by using the following keyboard combination:
ctrl / s
At this point we only have the table but no records inside it. Next, we will see how to create data.