‫So in this lesson, we're going to take a look at the relationship we have between our user and the
‫photos are related entity inside this class.
‫And we're also going to look at the Entity Framework conventions, because when we use entity framework,
‫a lot of things can happen conventionally just based on the code that we write here.
‫And we want a table in our database that's going to contain the photos, and there's going to be a relationship
‫between the user and the photos.
‫And we could say that one user can have many photos.
‫And in database terminology that would be referred to as a one to many relationship.
‫Now let's go and take a look at our data, context and insight here.
‫All we've got at the moment is a DB set for our users.
‫And we've already seen that Entity Framework created a table called users based on our code and the
‫DB sets that we added here.
‫So the question is, do we need to add another DB set for the photos?
‫Well, we could, but if we think about what we're actually going to be doing with these photos, then
‫when a user adds a photo, it's going to be added to that specific user.
‫A user is not going to be able to add photos for other users.
‫And at no point do we want a photo that's not associated with a user.
‫We also don't need to query the photos directly either.
‫We're not going to need to go to the database and say, Hey, can I get this one very specific photo
‫for a random user?
‫So based on that, then we don't actually need to create a DB set for the photos because we're not going
‫to be using that entity to query our database directly on it.
‫It's always going to be done via the user entity.
‫But I do like the tables inside the database to be pluralized.
‫And currently, if we use entity framework conventions, then it will go ahead and create a table for
‫photos.
‫But it's going to call it photo.
‫Based on the class name.
‫So we need to override that behavior for entity framework.
‫And the way that we can do that is we can add an attribute called table that comes from system component,
‫model, data, annotations, schema.
‫And then we can just specify what we want the table to be called in list attribute.
‫And we want it to be called photos.
‫So if Entity Framework is going to do all of this stuff for us conventionally, it's going to create
‫this table called photos.
‫It's going to set up the relationship between the app user and the photos.
‫Let's just go ahead and see what it gives us when we create a new migration based on what we've done
‫here.
‫Now we're using code first migrations, which means we can expect our migration to contain all of the
‫additional properties or the columns that it's going to create in a database or the user's table in
‫that database.
‫And we can expect to see that it's going to create a new table called photos as well.
‫And there's going to be a relationship between the photos and the user.
‫So let's go and take a look and see what actually happens when we do this.
‫Now, because we're creating a new migration, we do need to stop our API server from running and let's
‫go ahead and use the net F utility and say dot net F, and we'll say migrations at and we'll just call
‫this one extended user entity and press return.
‫And this should work without any problems.
‫And let's go and see what exactly has been created.
‫So we'll go to our data folder.
‫Inside the migrations folder, we can see that we have our new migration and let's take a look and see
‫what we have inside here.
‫So we've got our methods and it's going to add some columns.
‫The city, the country that created the date of birth, the gender, the interests, the introduction
‫known as last active looking for.
‫And it's also going to create a table called photos.
‫And it's going to give the table its ID, which is going to be an auto incremented property.
‫And what we also have in here is the app user ID.
‫Now, we did not add to this to our photo entity.
‫If we go and take a look at our photo entity, we do not have the app user ID in here.
‫But this is what I mean by entity Framework conventions, where it recognizes there is a relationship
‫between the photos and the user.
‫And to allow that, it means we have the app user ID in here.
‫Now the problem is that conventionally it's going to allow this to be null able.
‫And do we want to have a photo in our database that is not associated with a user?
‫And the answer to that would be no.
‫Of course, not all of our users or all of our photos should be associated with a user.
‫So this migration is not quite right for what we're doing.
‫So we're going to need to do some tweaking.
‫But let's take a look and see what else we have inside here.
‫We've got the primary key for our photos table and also the foreign key, which goes back to our users
‫table.
‫And it's using the app user ID as the foreign key to relate this table to the user's table.
‫But as I mentioned, we're not quite there because of lists.
‫This would allow us to have orphaned photos.
‫I call them orphaned photos, but photos that are not associated with a user.
‫So this is not quite right.
‫So we're going to have to remove this migration, but that's no problem because we've got a command
‫to do just that kind of thing.
‫And because we have not yet applied this to our database, we can say dot net if migrations and remove.
‫Now, when it comes to configuring our entities with Entity Framework, we do have a few options and
‫one of the options we can use is to use conventions to get entity framework to bend to our will.
‫And if we go and take a look at the documentation regarding relationships, we do have a section here
‫for conventions as well as manual configuration, and we're going to stick with conventions in this
‫case.
‫And as it tells us, a relationship will be created when there is a navigation property discovered on
‫a type.
‫Also, just to clarify this, when we have an entity or a type added to another class, then this is
‫referred to in entity framework terms as a navigation property.
‫If we want to get to our photos, we can say user photos.
‫And that's just a bit of terminology I just wanted to clarify here.
‫So the way that we can get nasty phone work to bend to our will and ensure that a user or a photo cannot
‫exist without a user is to fully define the relationships.
‫And if we take a look at their example here, they use blog and posts, and if we compare this to what
‫we are doing, this would be a user and photos as we've got the list of posts in here which would represent
‫the photos.
‫And the way that we fully define a relationship is inside.
‫The photo class I've highlighted post here, but inside our photo class we had the.
‫App user ID as well as the app user property, then that's going to fully define the relationship and
‫ensure that's that app user ID property is not null able.
‫So we would not be able to add a photo to our database without a corresponding user object for it to
‫be related to.
‫So let's go and take a look and we'll make that small change.
‫And in our photodex we'll add the relationship properties.
‫So we're going to specify prop of int of app user ID, and then we specify the prop of app user.
‫And specify this as app user.
‫So with that small change in place, let's go and create another migration.
‫We've removed the previous one, so I'm just going to repeat the earlier command I used and we'll say
‫add extended user entity.
‫And that goes ahead and creates our new migration.
‫Let's take a look at what it has created.
‫And inside here we'll scroll past all the columns it's going to create and focus on the photos table.
‫Now, this time we've got our app user ID, just as we had before in a previous version of this migration.
‫We've got our app user ID, but this time it's not allowed to be null able.
‫What we also have is the delete behavior.
‫And this is telling us that when we delete a user entity that's going to cascade down to its related
‫entity, which is the photos.
‫And that means the photos related to that user would also be deleted.
‫And this is the behavior we're looking for.
‫For our relationship, this is what we want to apply to our database.
‫So since we want to apply this to our database, let's go back to our terminal and we're just going
‫to use the dot net if database update commands.
‫Make sure this works successfully.
‫And that all looks pretty good.
‫We can see our tables being altered with columns being added, and we can also see the Create Table
‫command, which is also going to create our photos table.
‫And we can further confirm this by opening up our SQLite database.
‫So I'm going to say SQLite Open database.
‫Choose the database.
‫Go to our Solution Explorer and take a look at SQL Explorer and we can see that we've got our photos
‫table along with its properties or its columns, with the ID being the primary key.
‫And we can also see that our users table has been extended with the additional properties that we've
‫added inside there as well.
‫So now we have our tables updated in our database.
‫What we'll take a look at next is how we can seed data into it.
‫So we've actually got something to work with.