Okay, so now we've got our DB initializer class.
Let's just review this, make sure everything looks okay.
And we've got our init db.
And we create a scope using the using keyword.
And we use the app services Create scope.
Now when we using it this way and this services concept, it may become clearer once we start to look
at dependency injection.
Because what we're using here is a service that we've defined in our program class.
And I mentioned earlier, albeit very briefly, about dependency injection.
We haven't looked at that yet, and we cannot use it yet when we're doing something that involves the
program class, because dependency injection isn't available until our application is actually running.
Now, the point where we're going to use our DB initializer class is before our application has actually
started.
We're going to use this code just before we get to the run command.
After we've run our application, then our services we can use for dependency injection Projection purposes,
but we cannot do it where we're going to seed our data into the database.
Now we've got another ellipses here to tell us that we can do something.
And it tells us that member seed data does not access instance data and can be marked as static.
So okay, fine.
Let's do that and we'll use the ellipses.
And can we use the ellipses.
And well it's not letting us do anything automatically.
So perhaps we just need to say private static void here.
And then the warning or ellipses goes away.
And the ellipses are not warnings or errors by Luella.
Just advice to say, hey, you can do something slightly better here.
And in this case, when it's saying it doesn't need access to instance data, it means we're not injecting
anything into this class.
Therefore, this can be marked as static because it doesn't depend on anything else.
It's only depending or only using what's already inside this class.
So this can be what's referred to as a static method.
We'll get more into this syntax as we use it as we build our application.
So after we've got access to the context, we then call the Setdata method, which will then migrate
the database if necessary.
It's going to check for any pending migrations.
And then if it doesn't have a database then it will create the database and apply any pending migrations.
And that's going to happen every time.
So whenever we restart our app this method is always going to get called.
Then we're going to check to see if we've got any products.
And if we do have any products, we don't need to seed any more products in.
So we're just returning out of this method.
And then we create our new list of products and save them into our database.
So we need to call this method inside our program class.
And as mentioned we need to do this before our application is started.
So in order to do that we can use DB initializer and get that into our program class.
And we need to use the init db method.
Now I don't have access to the init db method in here, because I also need to make this a static method.
And a static method means effectively that I can use the method from this class without actually creating
a new instance of this class, so I don't need to say new DB initializer.
If I've got a static method in here that I'm calling.
So if I go back to the program class now, I've got access to the init db method and I can just pass
this.
It's required parameter, which is the app that we're using inside the DB initializer, the web application
app.
Now we've got access to the init db.
We can now go ahead and create our database.
But we've already got a database.
So let's remove that database so that we can see what happens when we restart our API.
To make sure that the database gets created and we seed our data in there.
So let's go back to the terminal.
And I'm just going to clear the screen here.
And please make sure your web application is stopped at this point.
And we're going to use the Net AF database.
And we can use the drop command which is going to remove our database.
So I'll press return and we should get a confirmation saying are we sure.
I'm going to say yes and press return.
And this drops the database.
And what that really means in the physical terms is our store DB is no longer part of our project files.
And then in order to create that database and see the data because of this line of code, then we can
just restart our app.
So I'm just going to say well clear the window and say dotnet watch and we'll keep an eye on the terminal,
the logs so that we can see what's happening.
But what should happen and what we should see is something like this.
We see a lot of inserts into products, which means our data is being seeded into the database.
And we can further confirm that by taking a look at Solution Explorer, clicking on the store DB.
And what we should see is that we've now got 18 products in our database.
As easy as that.
So great.
And next let's see how we can use this data to return to query our database and return this data from
the database in response to an HTTP request, which we'll take a look at next.