Entity Framework 6.1 came out with a feature called code first from database which allows you to reverse-engineer your database producing POCO classes from your tables (this was already present in Entity Framework as Database first approach). What is new if the fact that once the POCO classes have been created you can then switch to Code first, taking the current snapshot of the database as starting point of truth, from then onwards you can customize the database through code by applying migrations.
In the example below we’ll take the AdventureWorks 2012 database and we’ll use code first from database to create our classes, once we have imported the classes we’ll switch to Code First approach and we’ll add some data annotations to prettify our Classes names and properties as well as providing some extra validation.
Let’s start by creating a new web application in Visual Studio and selecting MVC as the template:
Once the project is created and depending on the version of Visual Studio that you have installed you may need to update Entity Framework to at least version 6.1, right click on your solution and select ‘manage Nuget packages for solution’, now select updates and finally select Entity Framework to check your installed version:
Ok, now it’s time to get stuff done, let’s reverse-engineer our database (I like how that sounds):
- Right click on your project and select Add -> New item
- Choose Data from the left menu
- Select ADO.NET Entity Data Model and add a name to your new model
- Click Add
- In the next step we select Code first from database.
- Click Next
- Setup your db connection
- And now select the tables and/or views that you want to add and click Finish
That’s it !. Entity Framework will now have mapped the selected tables/views to POCO objects (bear in mind that views are treated as tables) as well as setting up your DbContext and connection string in your Web.Config file.
An example of a mapped table is the Address class (see below how Entity Framework read the schema definition of the table and added the necessary data-annotations to ensure valid data is stored into the table)
If you haven’t seen the virtual keyword before it’s just the way how we can tell Entity Framework to lazy load in this case, the collection of BusinessEntityAddresses associated to a particular Address.
The last thing to do is to clean up property names to ensure a consistent name in our application (use the ‘Column’ data annotation to change the name of your property while maintaining the name of the column):
In a later post I will show how to use migrations to make changes to your model and have that reflected in your database
Below are great links about Code first from database:
- A comprehensive video about code first from database on Channel 9’s page here: http://channel9.msdn.com/Blogs/EF/Code-First-to-Existing-Database-EF6-1-Onwards-
- Julie Lermans’ Pluralsight video on Entity Framework 6: http://www.pluralsight.com/courses/entity-framework-6-ninja-edition-whats-new
- Also a link to the AdventureWorks database file: https://msftdbprodsamples.codeplex.com/releases/view/93587 for you to play with :)