Articles tagués Object Relation Mapping

Entity Framework 5.0 – Microsoft ORM ( Object Relational Mapping)

In this article, we will create a simple application using Entity Framework 5.0 with Code First approach. This approach is used when you want to create a database model from your application model.

Entity Framework is the ORM (Object Relationnal Mapping) in Microsoft Technologies.

First create a business object in a library class (the file is named Poco_entity.cs), client:

this is a POCO class because it doesn’t have an ORM depedency.

Secondly, create a clientContext class which will wrap the client class an is used by Entity Framework to create table

ClientContext inherits from DbContext and we use DbSet to wrap our POCO class.

It allows us to not have a dependency between class and ORM technologies. So ClientContext wrap a client collection.

Now we can create a database from our code application by initializing the database and the context for example in a console application:


Here, we first initialize the target database and indicate that each time the framework will drop and create database (only in development cycle;-). Secondly, we instantiate a ClientContext which allows us to manage our POCO class in clients collection (our DbSet property) to persist it in the target database. And finally, we call SaveChanges in order to create the database and record the client created in a table (client table).

Because of not specifying a sql connection, Entity Framework by default use SQL express:


Connect to your sql server express instance (for me Hassan-Tosh\Sqlexpress)

You will see that a database has been created for you (databaseContext.clientContext)

And in the client table, a new row is inserted 😉

you have also other approach: Database first ( you create entities code from an existing database ) and model first (you create class diagram in visual studio and Entity create database for you).

For more information:




, , , ,

Poster un commentaire

Microsoft System Center 2012

All System Center 2012 products, Service Manager, App-Controller, Orchestrator, Virtual Machine Manager, Configuration Manager, Data Protection Manager, Operation Manager -Boutougha Hassan

Microsoft Windows Server 2012

IT - Windows server 2012 - Hassan Boutougha

Microsoft Hyper-V

IT - Hyper-V - Hassan Boutougha

Microsoft SQL Server 2012

IT - SQL Server 2012 - Hassan Boutougha