In our application, we are going to save information about movies and actors who have acted in those movies.
We can think of two tables to do this – one for storing the movie information and another for storing the actor information.
How about the relationship between the two tables?
A movie can have many actors. And an actor can act in many movies.
So, can we say it as ‘many to many’ relationship between the two tables?
But there is a small problem here.
What if I want to have the actor’s role information? For example, Brad Pitt was playing the role of General Manager (Character name:‘Billy Beane’) in ‘Money Ball’ movie whereas he was acting as ‘Rusty Ryan’ in movie ‘Ocean’s Thirteen’.
Where can we save this role information?
We cannot save this information in Actor table – as it would mean that he was playing the same role in all his movies.
We cannot save this role information in Movie table as well – as this would mean that all actors in that movie would be playing the same role.
So, we need another layer of abstraction in between actor and movie tables – to store the role information.
Let’s call this Role – A movie can have many roles and actor may play multiple roles.
We can say that there is one to many relationship between movie and roles and many to one relationship between role and actor.
Enough theory. Let’s fire up Visual studio and code our model classes.
Right click Model folder on our solution and select Add -> New Item
When using Entity Framework ‘Code First’ approach, we define a class for each entity. We define the following classes in Model.cs (And I’ve deleted the generated code)
You might have noticed that we have not added anything related to Entity framework here.
Yes. It’s because these are just plain old C# classes which expresses the domain entities with the properties.
We’ll add DbContext class so that we can interact with the database using Entity framework.
Right click the Models and Select Add -> New Item.
A new file will be generated with the following content.
We are going to make following changes in this file.
- Include System.Data.Entity namespace as we are using Entity Framework.
- Make the generated class to inherit from Db
- Make Entity Framework to be aware of our domain classes
After making the above changes, your DBContext class would like below
Now, we want to display the list of movies when Index action method is accessed. Please note that no database is created till now for this application.
Let us what happens when we proceed to update the Index method to do the same.
In the above code, we are just getting the list of movies using an instance of MovieDBContext. The LINQ method db.Movies.ToList() method will generate a select sql and will try to run against the database.
As the database does not exist, Entity Framework will create a new database by name ‘MicroIMDBV2.Models.MovieDBContext’. The classes that you have made Entity framework aware would be created as tables. Properties of the classes would become columns of the table.
Please note that in Roles table, there is a foreign key to both Actors and Movie table.
Sidenote: I have explained the sequence of events that happen when you try to run a query on a non-existent database in Entity framework chapter.
Updating index view:
Once we fetch the data from database, we are the result(movie model) to the view of Index with the following code in Index action method.
In Index.cshtml, we are defining the model type that we would be sending from action method of controller. Then, we use this Model variable(note the capital M) to access the passed model – Here we are just looping through the Movie list using ‘foreach’ razor construct and displaying it in each line.
As we don’t have any data in database, no movie title would be displayed.
In the next chapter, we will discuss about how to insert the data into the database.