So what we're going to do now is tell entity framework about our relationship between our User class
and our photos clients.
And how do we go about doing that.
Well there are options.
And the way that I'm going to demonstrate now is to use convention to get entity framework to recognize
the relationship between a User class and the photos class.
Now as usual there's always more than one way to do something.
And there are options for configuring this for now I'm going to show you the nice and easy convention
based way to do so.
And a bit later on we'll look at always had configuring entity framework to scaffold our database in
the way that we want to.
So what we're going to do is tell entity framework about are relationship and do this.
We we've fully defined this relationship and at the moment we've just got a one way definition of this.
We've got a collection of photos inside our User class.
Now in order to fully define the relationship we need to do the inverse of what we've got in our User
class.
So all that means is we come into our photo class.
We added a property this time of property of user which we'll call user and will also add another property
on it.
And this will be the user I.D. Now just by doing this when we have another migration entity framework
is going to do things slightly differently.
Instead of having a restricted delete We now could have a cascade delete which means if a user was to
be deleted then our photos would also be deleted or the photo collection would also be deleted.
So let's go back into our terminal and will recreate our previous migration.
We deleted.
And this is the one to extend the User class.
So I'm just going to run this migration again.
And this is now complete.
And let's go ahead and take a look at our migration.
In this case.
So it's going to be pretty similar to what we had before except if we take a look at the photos table
where it's been created.
The first thing we see is the user I.D. is no longer available.
It's got a nullable of false and that was true before.
And what we also see is our undelete.
We now have referential ash action cascade.
And like I mentioned now if a user is deleted then the photos would be deleted as well.
So this is how we want our database to look and this is how we want our schema to be.
So let's go back into the terminal and do a dot net e.f. database updates and press return and that
complete successfully.
So let's just go to dbe browser for sequal lights and just refresh and we've got our migration applied
in here now.
If I take a look at my uses table my users is empty because I dropped some recreative my database.
If you didn't follow along you'll have some users in here.
But we got all of the columns that we are expecting.
And we've also got a table for our photos.
And if we go back to the database structure and we take a look at photos and modify table.
Then we can see that in our constraint where we've created this table we've got our undelete cascade
as we would expect from what we saw in our migration.
So our database or at least mine at the moment is looking pretty empty.
And what we're going to look at next is a way to populate our database with some data and begin to look
at a way of seeding data into this database.
That's all coming up next.