Setting up your models in MVC

 Sep 25, 2015

When we start creating MVC applications, one of the first steps is to create your models. These models are used as the entities we want to work with in our application and are commonly created as tables in a database.

When we want to create these models there are different approaches we can take, such as Database first or Code First. I want to go into the code first approach in this blog and talk about how we can create our models by using the Fluent API approach with Configuration classes.

The following piece of code is an example of how models will look:

setting up models in mvc setting up models in mvc

These two models are in a one-to-many relationship, this being one course and multiple modules per course.

When we create these models we can take more control over how they will be persisted in the database, to do this we can create a configuration class per entity. By creating there classes you can keep your code cleaner and more readable.

To create this class you have to add a new class to your project, this class should inherit from the EntityTypeConfiguration class and pass in the Class you want to configure as the type, finally create a constructor for that class. This constructor will be where we set all of the properties for our entity.

setting up models in mvc

For setting up your class you can use a struc called “Property” and the use a lambda expression on each property. After you have the property in question you can just append the property configurations.

In this first example we take Course ID and we set the following properties on it:

  • It is a required field, being not nullable
  • Must have a column name of c_id in the database
  • Must have a column type of integer
  • Must use the Identity function in SQL
  • Finally must be the primary key of the table

setting up models in mvc

The next field we configure is the course name field, and we set the following properties on it:

  • It is a required field
  • Must have a column name of c_name in the database
  • Must have a sqldata type of varchar
  • Must have a maximum length of 200 characters

setting up models in mvc

You can follow these steps throughout all the properties to set how they must be configured in the database

setting up models in mvc setting up models in mvc

Once you have coded all of your configuration classes for your entities you have to register them within the DBcontext class of your application. Within this class you will not only see your entities mapped to IDbSets, but you also have to override a method called “OnModelCreating”.

setting up models in mvc

Within this method we add our configuration by calling the add method on the configurations collection.

setting up models in mvc

By following these steps you can have full control over your entities as they are persisted to the back end.

For more information, have a look at New Horizons' Visual Studio Courses.

How do your Excel skills stack up?   

Test Now  

About the Author:

Auret Swanepoel  

As a recent addition to the New Horizons team, Auret is a highly skilled and qualified IT Technical trainer. He has been a Microsoft Certified Trainer (MCT) since 2008 and has since then, also become a Microsoft Certified Professional (MCP), a Microsoft Certified Technology Specialist (MCTS) and a Microsoft Certified Information Technology Professional (MCITP). With his international experience as a trainer in South Africa, Auret is able to adapt his teaching style to different audiences in the classroom and ensure that students are learning in a positive and collaborative environment.

Read full bio
top
Back to top