Posted on by & filed under Content - Highlights and Reviews, Programming & Development.

Microsoft shipped the latest version of Entity Framework (ver. 5.0) with Visual Studio 2012. You may be asking what is Entity Framework and what does it do for me as a developer? Why are we so excited about Entity Framework?

Entity Framework (EF) is an object-relational database mapper that enables easy database access (and CRUD etc.) for .NET developers using domain-specific .NET objects that EF can generate for you. If you have used other database access technologies from Microsoft in the past, you will be thrilled with how easy it can be to do the same type of database tasks using EF that you previously had to write a lot of code to do. And anything that can make life easier as a developer is a good thing.

As a developer working on Windows applications or web applications, you often have to deal with consuming data such as reading from a database or writing new information to a database. When starting out a new application you are usually faced with one of two options: you already have a database that your application needs to work against, or you have to create a new database for whatever task you are trying to accomplish in your application. Well, EF can “reverse engineer” your existing database and create the necessary classes and code to access the database, thus saving you a lot of coding that you usually have to do yourself. If you need to create a new database, EF can look at the classes in your code (i.e. the “model”) and create the necessary code for access. It can even create the database for you the first time it runs if it doesn’t exist already.  There are also two ways to do this, as you can use the EF Designer if you like dealing with a visual orientation using boxes and lines to map your data. Or if you prefer, you can do it entirely by writing code.

In this blog post we are going to use the Code First approach to define our database model, then let EF generate the database. Read Chapter 1. Welcome to Code First from Programming Entity Framework: Code First for more on Code First. In a later post we will look at using Code First to generate our database model from an existing database.

First, let’s open Visual Studio (I’m using VS 2012) and create a new Windows Console project and call it “SafariCodeFirst.”

Next, add the two classes to the code below the Program class. In a larger app, we would normally create a separate class or even add a new project to contain our classes. Note the virtual keyword that signals EF to do a lazy load of the data in the background when we first reference the context.

Notice that these are just two POCO (plain old CLR class) classes that describe what we want our database to look like, and there is nothing that mentions or refers to a database. Now we need to add Entity Framework to our project. We do that by selecting Projects -> Manage NuGet Packages and select the Online tab. Next, select the EntityFramework package and click Install and accept the license.

Add the following statement to the top of the code:

Then add the following class below the other two classes you added. This is derived from DbContext, which is a base type from Entity Framework. It represents a database session and allows us to query and save our data.

Now let’s modify our Main method in Program.cs as shown below so that we can use our new database. First, we create a new instance of the context (think database session/connection) and use it to insert a new Seminar. After we put some data into the database, we will run a LINQ query to retrieve all of the records in the database. Notice that we wrap our code in a using statement so that all of our resources will get cleaned up.

Now it’s time to run our application. As you can see, it first prompts us for the name of a seminar and then it loops through the database and writes out all of the seminars. The first time you run the application, Entity Framework will notice that the database doesn’t exist, so you will experience a slight delay while the database is created. You may also notice a slight delay as the data is written to the database. In the example shown, the app ran several times and added four records to the database.

At this point you may be asking “where is my data?” Well, that all depends. To see your data, open up Server Explorer, then right click on Data Connections and select Add Connection. You will get the window shown below. Here is where the “it depends” comes into play. If you are using Entity Framework 5.0, then your database should be in the localdb instance that is installed with EF 5.0, so you will use “(localdb)\v11.0” for the Server name and then use the SeminarContext that we created in our code. If you have Entity Framework 4.0, then you would connect to “.\SQLEXPRESS”.

After setting up the connection, you should be able to see your database as shown below.

That’s it. As you can see, using Entity Framework and Code First to analyze the classes in your project and generate the code to create and use the database is pretty straight forward.

Safari Books Online has the content you need

Check out these .NET Entity Framework books available from Safari Books Online:

Programming Entity Framework: Code First acts as an extension to Programming Entity Framework 2nd Edition. Code First is an additional means of building a model to be used with the Entity Framework and is creating a lot of excitement in the .NET development community. The book begins with an overview of what Code First is, why it was created, how it fits into the Entity Framework and when to use it over the alternatives.
Entity Framework 4.1: Expert’s Cookbook holds many examples to help guide you through tough technical decisions and avoid technical landmines. The book will guide you from just using Entity Framework to enhancing the data access wizard. You will be guided through complex mapping scenarios, query definition, reusability, integration with other technologies, and architectural management. The approach is step-by-step and test driven so that it is focused as much as possible on solving problems and getting the most out of the time spent working through the book.

About the author

Marshall Harrison is the founder of GotSpeech.Net and GotUC.net as well as the owner and CEO of GotSpeech Consulting LLC. He holds a B.S. degree in Computer Science and was a Microsoft MVP from 2006 – 2011. He is currently focusing on Microsoft Lync as well as Windows 8 and Windows Phone 8 devleopment. He can be reached through his blog at GotspeechGuy.com.

Tags: .NET Entity Framework, Code First, LINQ, Microsoft Visual Studio, POCO,

Comments are closed.