Entity Framework Basics

What is Entity Framework (EF)?

Microsoft has defined the Entity Framework in following words:

The Microsoft ADO.NET Entity Framework is an Object/Relational Mapping (ORM) framework that enables developers to work with relational data as domain-specific objects, eliminating the need for most of the data access plumbing code that developers usually need to write. Using the Entity Framework, developers issue queries using LINQ, then retrieve and manipulate data as strongly typed objects. The Entity Framework’s ORM implementation provides services like change tracking, identity resolution, lazy loading, and query translation so that developers can focus on their application-specific business logic rather than the data access fundamentals.

It is an enhancement to ADO.NET that gives developers an automated mechanism for accessing & storing the data in the database.

ORM is a tool for storing data from objects to relational database like MS SQL Server in an automated way without much programming. Other well known ORM frameworks for .net are DataObjects.Net, NHibernate, OpenAccess, SubSonic etc. ADO.NET Entity Framework (Open source) is from Microsoft.

Please note: Entity framework not only supports MS SQL Server, but also other database like Oracle, DB2, MySQL etc.

Types of Entities in Entity Framework:

1. EntityObject

2. POC (Plain Old CLR Object)

3. POCO Proxy

4. Self-Tracking Entities.

Each entity can have two types of properties, Scalar properties and Navigation properties.

Scalar properties are properties whose actual values are contained in the entity. Whereas Navigation properties are pointers to other related entities.

Modeling techniques using Entity Framework 4.1

1. Code first

In the Code First approach, You write your classes first and then create database from these classes. There are two new objects introduced for Code First approach, DbContext and DbSet. DbContext is the primary object for interacting with a database using a specific model. DbSet(Of TEntity) is used to perform CRUD (Create, Read, Update and Delete) operations against a specific type from the model in Code First approach.

2. Model First

In Model First approach, you create Entities, relationships, and inheritance hierarchies directly on EDMX File. So in Model First approach, when you add ADO.NET Entity Data Model, you should select ‘Empty Model’.

After creating required entities, associations and inheritance on design surface of the empty model, you can use designer’s context menu option ‘Generate database from model’ to generate the database.

3. Database First

In the Database First approach, you will reverse engineer a model from an existing database. The model is stored in an EDMX file (.edmx extension) and can be viewed and edited in the Entity Framework Designer. The classes that you interact with in your application are automatically generated from the EDMX file.

We will see examples of this approaches in upcoming articles to clear the terms more better.

Please comment below for any doubts or questions.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s