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:
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.
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
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
You can follow these steps throughout all the properties to set how they must be configured in the database
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”.
Within this method we add our configuration by calling the add method on the configurations collection.
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 NowNext up:
- Create a Chart Template in Excel
- Cascaded Dropdowns in Nintex Forms
- The Aussie Meat Pie of Managing Teams
- Introduction to Office 365 Video
- Entity Framework Code-First
- The awesomeness that is AngularJS - Part 1
- What Customers Want
- Deleting an undelete-able site collection
- Non-Breaking Spaces & Non-Breaking Hyphens in Word
- Creating a simple Website and User Interface with MVC - Part 1
Previously
- No need for meeting notes anymore!
- “If you can’t measure it…”
- The best features in Office 2016!
- Use a slicer to make better business decisions
- What's a JavaScript Closure?
- Put that marker down and Redact in Acrobat please!
- Understanding your customers with 6 questions
- New security features in Windows 10
- All About VLOOKUP() in Excel
- Creating a layout for Word and Excel