Entity Framework - Model First

Microsoft's Entity Framework provides developers with several methods for creating underlying data structures for their applications. The Model First approach provides an easy to use design interface for visually mapping out entities and relationships.

In the following examples I am using Visual Studio 2013, but 2010 and 2012 provide most of the same features. I will be using the Entity Framework Designer to create a data model for an IT department inventory system.

To begin, you just right click on the design surface and select Add New >> Entity
I'm calling this base entity InventoryItem and leaving the Create key property checkbox checked and leaving the Property name: field set as the default (Id). It will end up looking something like this:

Now, to add more properties you just right click the entity and select Add New >> Scalar Property. After adding a few more properties the design surface will look like this:

In this screenshot you can see the properties for the Price property. In the properties window you can set the default value, type, precision, scale, etc. The InventoryItem entity is complete for now so now we can create a few more entities.

Now we have three entities and the relationships between them defined:

The InventoryCategory entity represents broad types (e.g. Laptops, Printers, Scanners, etc). The InventoryType entity represents more specific types (e.g. Dell Precision 4700, HP LaserJet 2600n, Epson GT-1500, etc). And the InventoryItem will represent a specific instance of an InventoryType.

Adding a relationship is as simple as right clicking an entity and selecting Add New >> Association.

From this window we are able to select which entities we want to define a relationship for and the type of relationship. In this case we are creating a one to many relationship. Now we can go ahead and build out the rest of our data structure.

Once the data model is complete when can generate a the SQL script to build this database by right clicking the design surface and selecting Generate Database From Model... This will open up a wizard that will require the SQL server connection information and it will allow you to choose a database name. Once the wizard finishes, you will end up with a script that looks something like this:

Just open up the script and click the green 'play' button at the top of the window and you will have a database ready to go! There are also classes generated for each entity:

And this where the most powerful parts of the Entity Framework come in -- using LINQ to interact with our newly created data model. But I'll cover that in a future post.

Model First is an excellent tool for quickly designing the data structure for an application. Making changes to the model becomes a trivial task since all the generated classes are automatically updated. It has made dramatic improvements to my workflow since I'm no longer switching back and forth between Visual Studio and SQL Server Management Studio while building applications.

comments powered by Disqus