Articles‎ > ‎

[ Article ] How to configure and use Entity Framework 4.1 using visual studio 2010

ADO.NET Entity Framework

Introduction

This article is all about working with relational data using domain-specific objects. We will use Entity Framework to achieve this. ADO.net Entity Framework is an object relation mapping framework for the .NET Framework. It enables .NET developers to work with relational data using domain-specific objects. It eliminates the need for most of the data-access code that developers usually need to write. In this article we will see how to configure and use Entity Framework 4.1. This article will explain a Model First approach of configuring and using Entity Framework 4.1.

Background

I was working with Entity Framework and I faced problems in my initial stages of configuring Entity Framework 4.1. This article could help you in the initial stages of configuring and using Entity Framework 4.1. I have not included extensive use case of Entity Framework. Here I will show you Configuration of Entity Framework and a simple example of inserting values to database which you have configured with Entity Framework.

Requirements

For confuguring Entity Framework 4.1, You dont need much prerequisites.

Table of contents 

  1. Configuring Entity Framework 4.1
  2. Model First Approach 
  3. Entity data model
  4. Consuming Entity Framework
  5. Hurdles

Using the code

Configuring Entity Framework 4.1

There are different ways to configure Entity Framework. A new Model First approach was supported in Visual Studio 2010, which was released together with the second Entity Framework version (Entity Framework v4)

To begin with, lets have a look at how to configure Model First approach of configuring and using Entity Framework 4.1.


Model First Approach

In Model First approach the development starts from scratch. At first, the conceptual model is created with Entity Data Model Designer, entities and relations are added to the model, but mapping is not created. After this Generate Database Wizard is used to generate storage (SSDL) and mapping (MSL) parts from the conceptual part of the model and save them to the edmx file. Then the wizard generates DDL script for creating database


Visual studio supports many features so that we can easily add Entity Framework to our project. To Add Entity Framework to a project you will need a package manager. Lets see how to add a package manager extension to visual studio. To do this select the “Extension Manager” option from the tools menu. In Visual Studio, go to tools menu and select Extension manager as shown in the image below.


How to add extension manager to visual studio

Now to the window that pops up, select the “online gallery”. Now to the search box on the right top corner, type “Nuget Package” as shown in the image below. The window then will list the Nuget Package manager. Click on the download button. This will install the NuGet Package manager.


How to install nuget package manager


Done! your package manager is installed. Now you can start install entity framework. After you have installed Nu-get package manager, you have two options to install entity framework.Either you can install Entity Framework using package manager console or purely via UI.

Installing Entity Framework Via package manager console

For installing via package manager console, you have to select the package manager console from the “View” menu in visual studio. For this in visual studio, go to View then select Other Window from there select Package Manager Console.Package manager console will be available once you have successfully installed the package manager extension for visual studio. Now to the console which you can see inside visual studio as shown in image below, type "Install-Package EntityFramework" and press enter. It will start Installing. If you are behind any firewall and you need credentials to access internet then it may ask for that credential. Give your credentials to connect to internet and it will start downloading and will install.

Installing entity framework via package manager console

Installing Entity Framework purely via UI

Installing Entity Framework purely via UI is very Simple and sometimes handy too. You can install Entity Framework and add all required dll references to your project in a single go if you are using the UI way of installation. Also this way of installation will automatically add the required configuration into your applications configuration file. To install Entity Framework purely via UI, right click on the projects Reference and there you can find an extra option "Manage NuGet Packages. This option is only available if you have installed package manage extension for visual studio. Now click on this option as shown in the image.

Manage NuGet Package manager option in visual studio

Once you have selected the option,it will take you to another window where you can find many frameworks for .Net along with Entity framework. Now select Entity Framework and click on the Install button to install it.

Installing Entity Framework via UI

With this,installing Entity Framework is over. After the Entity Framework is installed, you can see some entry in your App.config file which are associated with the Entity model. Now lets see how to consume Entity Framework.

Entity data model

The Entity data model specifies the conceptual model (CSDL) of the data via the Entity-Relationship model, which deals primarily with Entities and the Associations they participate in. The EDM schema is expressed in the Schema Definition Language (SDL), which is an application of XML. In addition, the mapping (MSL) of the elements of the conceptual schema (CSDL) to the storage schema (SSDL) must also be specified. The mapping specification is also expressed in XML.

Visual Studio also provides Entity Designer, for visual creation of the EDM and the mapping specification. The output of the tool is the XML file (*.edmx) specifying the schema and the mapping. Edmx file contains EF metadata artifacts (CSDL/MSL/SSDL content). These 3 files (csdl, msl, ssdl) can also be created or edited by hand.


.edmx File

An .edmx file is an XML file that defines a conceptual model, a storage model, and the mapping between these models. An .edmx file also contains information that is used by the ADO.NET Entity Data Model Designer (Entity Designer) to render a model graphically. The recommended practice for creating an .edmx file is to use the Entity Data Model Wizard. Changes are made to an .edmx file when you use the Entity Designer to modify your model and when you use the Update Model Wizard to update your model based on changes to the underlying database.


Consuming Entity Framework

Now let’s see how to consume Entity Framework in our project. For using Entity Framework in our project, we need a data model. After installing Entity Framework, we will get one new template which is called "ADO.Net Entity Data Model". Entity data model will automatically generate code to manage our data models. Let’s see how to create "ADO.Net Entity Data Model". For this, you have to add new item to your project via “Add new item” wizard and select "ADO.Net Entity Data Model". This will ask you to Select your database based on your setting in you app.config file. So before configuring Entity Framework, Please configure your database for model first approach. Here you have the option for configuring without any database, but as we are configuring for model first approach it is recomended to configure your database before you configure Entity Framework. After adding ADO.Net Entity Data Model, you can find a new file in your project with an extension of ".edmx" when you click on that file you can see the Entity designer. At this stage your project has the entity model; also this will add some entry in your App.config file under connection string tag.

Step1. Adding ADO.Net Entity Data Model :- 

Adding Entity Browser

                    

  And at the add new item wizard, Select "ADO.Net Entity Data Model".


Adding new item


This will ask you to Select your database based on your setting in you app.config file. So before configuring Entity Framework, Please configure your database for model first approach. Here you have the option for configuring without any database, but as we are configuring for model first approach it is recomended to configure your database before you configure Entity FrameworkAfter adding ADO.Net Entity Data Model, you can find a new file in your project with an extension of ".edmx" when you click on that file you can see the Entity designer.


Entity Browser

At this stage your project has the entity model, also this will add some entry in your App.config file under connection string tag.

App settings

At this stage, Installing and configuring Entity Framework is over. Now lets see how to use it in our code.           

           

var ob = new ApplicationSetting {bootOnStartUp = true};
            using (var obj = new ProductivityEntities())
            {
                obj.ApplicationSettings.AddObject(ob);
                obj.SaveChanges();
            }


Here ApplicationSetting is the name of my table which has a corresponding Entity class while we have configured ADO.Net Data Model. ProductivityEntities is the class corresponding to the entry in your app.config file for connection string where the name of the tag is "name=ProductivityEntities". After executing this line, your table will have an entry with the fields filled with data that you have supplied. Here in my case my table have only one filed and the name of the field is "bootOnStartUp " which is of type boolean. For every field in table, you can find corresponding property in corresponding class for Entity.


Hurdles

1. If you are using .net version 4.0 there can be a chance of getting an error as below.

Additional information: Mixed mode assembly is built against version 'v2.0.50727' of the runtime and cannot be loaded in the 4.0 runtime without additional configuration information.

Visit here to find out the solution Mixed mode assembly is built against version 'v2.0.50727' of the runtime and cannot be loaded in the 4.0 runtime without additional configuration information.


Comments