Okay.
What we have at the moment is an empty database, and we want to correct that and have some demo products
so that we can lay out our user interface when it comes to building our client application, so on and
so forth.
So let's deal with that.
Now let's close down this store and inside our data folder let's create another new class.
So I'm going to say new file select class and we'll call it DB initial iser and press return okay.
So in this class then we're going to create some code so that we can do something with our DB context,
because we're going to need to save a bunch of products into our database.
And where we're going to use this DB initializer class is actually in our program class.
So we're going to do something inside here.
And we're going to create a method inside here and say public void.
Because after public comes what are we going to return from this method.
Well, we're not going to return anything from it.
And I'm going to call it init db.
And in here we're going to pass in as a parameter web application app.
And the reason for that, let's just open up the program class at the same time as we want access to
this effectively.
And I'll explain why as we build this particular class so that we can seed some data in there.
So we're going to use something called a using statement here.
And I'm going to say var scope using var scope equals app.
And this will give us access to our services.
And I can say create scope.
So what's this using statement all about then if well if we hover over this then we can see that this
has got a dispose method.
And this performs application defined tasks associated with freeing, releasing or resetting unmanaged
resources.
So what this effectively does is anything that we use inside scope, the thing that we've called scope
is going to be disposed of by the framework after we've finished using it.
And we don't need to clean up ourselves.
Now, dotnet is very good at cleaning up resources that are no longer in use.
But by using this using statement, we ensure this happens as soon as it possibly can, rather than
something called a garbage collector coming along at some future point which we do not have control
over.
It will clean up any unused resources, but this just guarantees that after we finished with this resource,
it will be cleaned up on our behalf.
Then I'm going to create a variable called context and say var context equals scope dot service provider.
And we're going to say get required service.
And I'm going to specify the store context.
And this is going to give us access to our store context.
Now we have to do this in a slightly different way.
And because this is a service that's been defined in our program class in this code, then we need to
get hold of that service via the framework, via the scope that we've been created or we have created
up here.
So this gives us access to our database context.
And then I'm going to create a method called seed data and pass in the context.
And if we hover over the context, then we can see that this is a type of store context.
And it's got the optional property there.
And that's not ideal because we don't want to try and use this if it could be null.
So after the or instead of the semicolon here let's just go to the next line.
We'll add a double question mark.
And this tells dotnet what to do if the context is null.
So if it is null then we're just going to throw a new exception and we'll just say throw new invalid
operation exception because we cannot see data if we don't have our context.
And I'm just going to say failed to retrieve store context.
And then we've got our seed data method.
Now this doesn't exist yet.
So let's put our cursor inside seed data and use the quick fix to generate method seed data.
And inside here, because we've now got access to our context, what we can do is we can migrate our
database or update our database inside code.
So I'm going to specify context.
And then we've got access to our database.
And we can use the migrate method.
And if we hover over the migrate method then it tells us what it's going to do.
This is going to apply any pending migrations fuller context to the database.
And it will create a database if it does not already exist.
So this will create and apply migrations to a database.
Now we're also going to check to see if we have any products already in our database.
So we'll add an if statement and we'll say if context dot products dot any, then we are simply going
to return from this method and do nothing with it.
So this return statement here, this is going to stop the execution of any more code inside this method.
And below this we're going to create a new list of products.
And we can say var products equals new list.
And the list is going to be a type of product.
And inside here we open curly brackets.
So I'll go on to the next line.
Open curly brackets and inside here we can start defining a new list of products.
So I could say for instance, new products and then open curly brackets.
And then I could start populating the different fields.
So I could say id one, I need to be a comma.
We could say the name equals test.
We could say description equals whatever and carry on going like this.
But we need a bunch of products.
We don't just need one.
And it's very tedious to start typing out all of this seed type of code.
So we're not going to do that.
I have provided a snippet for us to use that has a bunch of products that we can use for demonstration
purposes.
So let's go to the course assets folder.
Please go back to lesson three if you haven't already got this, and make sure you download the course
assets.
Inside here is a file called products dot txt.
If we open this then this just has a bunch of new products.
And if I just control a control C and go back to VSCode and paste this in.
And then just add the closing semicolon there and make sure things look okay.
Then we can see that we have a bunch of new products.
The formatting didn't work quite how I wanted it to.
I'd like it to be shifted over slightly to the left, and the auto formatting, which we should be able
to access via right clicking, and then just say format document.
And there is a shortcut key for this as well.
That should have shifted things over to the left a bit, but it didn't.
So I'll just do that manually.
And then we've got our bunch of new products inside here.
Now there is a hint here about, hey, this could be simplified.
And if we do take the opportunity to look at how it can be simplified, we can use the use new.
And there's also a fix all here.
So let's take a look at using that and say document.
And this simplifies our code and takes out all of that new product.
And this is something that was available in dotnet eight just to remove some of the boilerplate.
That's not necessary when creating a new instance of something.
So there's a slightly cleaner way to do this now.
So our VAR products, we want to save this list of products into our database.
So below the list of products let's bring the cursor down below that below the semicolon.
And we'll say context dot products dot add range so that we add the range of products in that list.
And we'll specify products.
And then we'll say context and save changes.
And the way that Entity Framework works is everything here and upwards.
All it's doing is it's tracking this list of products in memory.
And it's only when we get to the context save changes that we actually make a request to the database
at this point.
So that's our seed class.
In the next lesson we'll take a look at how we can use this.
And we'll seed the data into our database and we'll make sure it exists.
And we'll take a look at that next.