Join Ervis Trupja for an in-depth discussion in this video Creating a database, part of Building Angular and ASP.NET Web API Apps.
- [Instructor] On the last part we learned how to declare a constructor and how to get a connection stream from our database. On this part we're going to learn how to add a migration, add some test data, and then run the application to see if everything works as expected. So, let's get started. The reason why we need to add a migration, is because in case, in a later step we want to change the database schema. We can change it without losing the data. We have all the models within the models folder. And so far we have only the entry model.
We have said that, all the models will be translated into tables in our Escrael Surver. But, we know that each table has a unique column, which contains all the primary keys. But, how can we define a primary key in our model? To define a primary key, we need to use the key decorator. So, let us first create the column, for that I will just write prop, double tab, integer. I name my column Id, and then up here, I define this to be my key column or the column that will contain all the primary keys.
And then of course, I need to use the DataAnnotations name space. So, everything is set up on the entry part, let us go and add our first migration. Before you add your first migration make sure that you have, enabled already the migrations. By typing here enable migrations. So, the migrations have been enabled. Now, let us add our first migration. Add dash migration and than name your migration whatever you want, I will name my migration in this case Initial, and then press enter.
So we see that when you add a migration, a migrations folder is created. And inside this you have too many files. The configuration.cs file, which holds the configuration and the initial dot cs file and the initial is the same name as our migrations name, because for every migration, a new file will be created and inside this file, you will see all the changes. So in this case, inside my migration, I will see that, I will create a table and this table has the name Entries, and inside the table Entries I have the Id, which is not a nullable because is the primary key.
I have a description, I have a IsExpense and a value column. And the PrimaryKey is set to be the Id column. Now, for us to have some data in our data base, when we first run our revocation. we need to go to the seed method, and you can find the seed method within the Configuration.cs file. So, here let us add some dummy data. I'll just write in here, context, dot entries, dot add, and then add a new entry.
So, new entry. (keyboard typing) And this entry will have a description, which will say test. It will have a IsExpense value, which will be false and it will have a value, which will be 10 point 11. And don't forget the semi columns, at the end of this line. You can remove the models from here, and then just import the necessary name space, and save the changes.
Now to update the database with with this changes, we need to execute another command. So in here we need to write, update dash database and optionally, we can use, another flag. If I write in here dash V, which stands for variables. It means that the changes will be automatically applied in my database, without even starting the application or we can leave this out. It means that the changes, are not applied, until I start this application.
So I'll just press enter. So the seed method has been running, now let me run the application. And the application ran successfully. Let us go to our database, so for that go to the server explorer tab, inside the data connections. Refresh the data connections, because we are already connected. Then expand this database, and inside the tables folder, you will see our entries table. To see the data, simply right click and then go to the show table data.
So here we have the data, that we added with our seed method and the Id is the primary key, it's automatically generated by the database
- Setting up the app infrastructure
- Creating a database
- Configuring routing in Angular
- Designing a view with Material Design
- Updating existing data
- Creating a Web API HttpPut endpoint
- Deleting data
- Authentication using tokens
- Adding a filter, pagination, and icon buttons