Data Modeling

Sidenote : This is the part 9 of the ASP.Net MVC course series.  You can get all the parts of this tutorial here.

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?

DataModel 1

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?

DataModel 1.1

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.

DataModel 2

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

 

Model Class - 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)


public class Movie

{

public int Id { get; set; }

public string Title { get; set; }

public int Year { get; set; }

}

 

public class Actor

{

public int Id { get; set; }

public string ActorName { get; set; }

public string ShortBio { get; set; }

}

 

public class Role

{

public int Id { get; set; }

public string RoleName { get; set; }

public virtual Movie movie { get; set; }

public virtual Actor actor { get; set; }

}

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.

Adding DbContext:

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.

MovieDBContext - Add New Item

 

A new file will be generated with the following content.


public class MovieDBContext

{

}

We are going to make following changes in this file.

  1. Include System.Data.Entity namespace as we are using Entity Framework.
  2. Make the generated class to inherit from Db
  3. Make Entity Framework to be aware of our domain classes

After making the above changes, your DBContext class would like below


using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Data.Entity;

 

namespace MicroIMDBV2.Models

{

public class MovieDBContext : DbContext

{

public DbSet<Movie> Movies { get; set; }

public DbSet<Actor> Actors { get; set; }

public DbSet<Role> Roles { get; set; }

}

}

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.


// GET: Movie

public ActionResult Index()

{

using (var db = new MovieDBContext())

{

var movies = db.Movies.ToList();

return View(movies);

}

}

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.

Table structure

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.


return View(movies);

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.


@model IEnumerable<MicroIMDBV2.Models.Movie>

@foreach(var movie in Model)

{

@movie.Title<br/>

}

In the next chapter, we will discuss about how to insert the data into the database.

Previous Chapter  Home Next Chapter

[x_subscribe form=”398″]