Tag Archives: framework

Introduction to Microsoft Entity Framework 4.1 in VS 2010–Part 1

Today, what I came across with was quite a new thing. Entity Framework is not that old thing for guys who use to work on Data Access through Microsoft Standards. But Entity Framework 4.0/4.1 has brought ORM (Object Relational Modeling) within them.

Linq To Sql was the ORM we used to access databases. I am going to discuss here, Entity Framework 4.1 in VS 2010.

So lets get started, for setting up your environment. Download the ADO.NET Entity Framework 4.1. Install the package and it will work with VS2010.

First, we are going to make a Class Library Project named CLEntityFrameworkDemo

image

this is how the Solution Explorer will look like:

SolutionExp

We are going to delete Class1.cs, and add New Item…

Add-Item

In this dialog, On the left we selected Data, and in data we are going to add ADO.NET Entity Data Model and name this Model MyModel.edmx.

NOTE: edmx stands for Entity Data Model XML (we will discuss why this XML is here, so don’t worry Smile )

image

As soon as we add MyModel.edmx, it asks whether we want and Empty Model or we already have a Database to which we want to Model.

image

The Empty Model lets you to insert Entities right inside the Entity Model designer and you can create database based on the Entity Model you have created. So now you can make your entities, define their relationships (0..1 to many, 1 to many, many to many etc.) and create database based on those entities.

We are going to select Empty Model for now, I just want you to show how we can create entities and then create a database from them. And this is how your VS will look like after adding:

image

Toolbox: We have got Association (Relationship), Entity (Object Model) and Inheritance. So now you can imagine a picture of what its all about Winking smile

Solution Explorer: The main reference we need to have is the System.Data.Entity class which is necessary for this. Note the MyModel.edmx has MyModel.Designer.cs file with it. So its just like we have dbml file in Linq to Sql having a Designer.cs file.

We will cover what we have in the Designer class so that we can play around with it. For now, lets make entities for our demo. You can drag and drop Entity from Toolbox.

image

I have changed name from Entity1 to Customers. Notice the properties we have Abstract = False, wow… So we can make Abstract Entities here… For Example, we have People’s Abstract Entity, and we can have Customers and Vendors having Base Type as People. Also, we do not have to care about database, it will be created by this Model automatically Smile. Here is the sample model:

image

I have changed Customers “Id” attribute to “CustomerId”, since we cannot have same attribute names as that of Base Type entity. Now we can generate database from this model, lets try this… Right click on Entity Designer and click ‘Generate database from model’.

Generate-db

This will open up the database connection dialog…

Connection

Notice that the entity model has its own kind of connection string, containing the Database connection string as a part of it. Click next:

  1:
  2: -- --------------------------------------------------
  3: -- Entity Designer DDL Script for SQL Server 2005, 2008, and Azure
  4: -- --------------------------------------------------
  5: -- Date Created: 04/26/2011 13:59:20
  6: -- Generated from EDMX file: D:\PROJECTS\BLOGS\CLEntityFrameworkDemo\CLEntityFrameworkDemo\MyModel.edmx
  7: -- --------------------------------------------------
  8:
  9: SET QUOTED_IDENTIFIER OFF;
 10: GO
 11: USE [EFBlog];
 12: GO
 13: IF SCHEMA_ID(N'dbo') IS NULL EXECUTE(N'CREATE SCHEMA [dbo]');
 14: GO
 15:
 16: -- --------------------------------------------------
 17: -- Dropping existing FOREIGN KEY constraints
 18: -- --------------------------------------------------
 19:
 20:
 21: -- --------------------------------------------------
 22: -- Dropping existing tables
 23: -- --------------------------------------------------
 24:
 25:
 26: -- --------------------------------------------------
 27: -- Creating all tables
 28: -- --------------------------------------------------
 29:
 30: -- Creating table 'People'
 31: CREATE TABLE [dbo].[People] (
 32:     [Id] int IDENTITY(1,1) NOT NULL,
 33:     [Name] nchar(50)  NOT NULL,
 34:     [Address] nvarchar(max)  NOT NULL
 35: );
 36: GO
 37:
 38: -- Creating table 'People_Customers'
 39: CREATE TABLE [dbo].[People_Customers] (
 40:     [CustomerId] int IDENTITY(1,1) NOT NULL,
 41:     [Id] int  NOT NULL
 42: );
 43: GO
 44:
 45: -- Creating table 'People_Vendors'
 46: CREATE TABLE [dbo].[People_Vendors] (
 47:     [VendorId] int IDENTITY(1,1) NOT NULL,
 48:     [Id] int  NOT NULL
 49: );
 50: GO
 51:
 52: -- --------------------------------------------------
 53: -- Creating all PRIMARY KEY constraints
 54: -- --------------------------------------------------
 55:
 56: -- Creating primary key on [Id] in table 'People'
 57: ALTER TABLE [dbo].[People]
 58: ADD CONSTRAINT [PK_People]
 59:     PRIMARY KEY CLUSTERED ([Id] ASC);
 60: GO
 61:
 62: -- Creating primary key on [Id] in table 'People_Customers'
 63: ALTER TABLE [dbo].[People_Customers]
 64: ADD CONSTRAINT [PK_People_Customers]
 65:     PRIMARY KEY CLUSTERED ([Id] ASC);
 66: GO
 67:
 68: -- Creating primary key on [Id] in table 'People_Vendors'
 69: ALTER TABLE [dbo].[People_Vendors]
 70: ADD CONSTRAINT [PK_People_Vendors]
 71:     PRIMARY KEY CLUSTERED ([Id] ASC);
 72: GO
 73:
 74: -- --------------------------------------------------
 75: -- Creating all FOREIGN KEY constraints
 76: -- --------------------------------------------------
 77:
 78: -- Creating foreign key on [Id] in table 'People_Customers'
 79: ALTER TABLE [dbo].[People_Customers]
 80: ADD CONSTRAINT [FK_Customers_inherits_People]
 81:     FOREIGN KEY ([Id])
 82:     REFERENCES [dbo].[People]
 83:         ([Id])
 84:     ON DELETE NO ACTION ON UPDATE NO ACTION;
 85: GO
 86:
 87: -- Creating foreign key on [Id] in table 'People_Vendors'
 88: ALTER TABLE [dbo].[People_Vendors]
 89: ADD CONSTRAINT [FK_Vendors_inherits_People]
 90:     FOREIGN KEY ([Id])
 91:     REFERENCES [dbo].[People]
 92:         ([Id])
 93:     ON DELETE NO ACTION ON UPDATE NO ACTION;
 94: GO
 95:
 96: -- --------------------------------------------------
 97: -- Script has ended
 98: -- --------------------------------------------------

 

and it will generate a .sql file for creating all the entities. Cool isn’t it?

Lets look why edmx stands for Entity Data Model XML so why we have this XML here… Right click on the MyModel.edmx file in Solution Explorer and click Open With…

EDMX-Openwith

then select XML Editor….

image

At the first glance the XML will look quite complex

generated-xml

So, the edmx is actually an XML file, that’s why we have XML with edm Smile. Also, look at the above image, we have got SSDL Content i.e. the Storage Model, and CSDL Content i.e. Conceptual Model. Both are separately defined, Conceptual Model contains the entities and their relationship etc. information and Storage Model contains information on how to create these entities in database and how to cater relationships between entities in database.

That’s all for the first part Smile, in future parts we will look how to use this assembly to apply CRUD operations on database using this edmx we have generated.