Archive

Posts Tagged ‘Entity Framwork’

Why to use entity framework(EF)

March 26, 2014 Leave a comment

As we know that there are numbers of data access technology available these day like ADO.NET , LINQ to SQL and NHIBERNATE that working fine then why does any of us want to adopt new technology , why should I adopt or learn this new technology or rather what problem they sort out that existing technology could not efficiently . So lets explore these technology the way these let us connect and work with database then the answer of why to use EF will come out.

Entity Framework vs ADO.NET

ADO.NET

  1. You write code against Database object like table , store procedure etc.
  2. You managing the connection life like (open, close) to database .
  3. If you change databse in future then you have to change your code specific to that provider .

Entity Framework(EF)

  1. You write data access code against domain object rather than database object
  2. Entity framwork managing the whole things like creating database connection , executing query , processing result , tracking changes to the object so you are totally focusing on you r business logic rather than database things hence amount of code you have to write for database operation is reduced .
  3. In EF , query is written the form of LINQ ,Entity SQL and then it translated by provider to specific back end query syntax so if you change database in future then there are not much impact.

So by going through above comparision between EF and ADO.NET its clear that EF has great potential as data access technology over ADO.NET 🙂

Entity Framework vs LINQ to SQL:

LINQ to SQL

  1. LINQ to SQL support only SQL SERVER.
  2. LINQ to SQL class supoprt one-to-one map with database objects like Employee class can be mapped only with Employee table.

Entity Framework

  1. Entity framework has support for full provider .
  2. You can map your domain class with numbers of table using feature TPT , TPH of EF.
  3. There are number of way you can use EF like Database first , Model first, code First.
  4. It is open source and Microsoft soft has long term strategy for this technology.

So if you reading from top top to this line then I think it is clear to you now that WHY we would use Entity framework over other technology .

I would be happy to hear your feedback , correction in this article ,enhancement to this article .

 

Advertisements
Categories: Entity framework Tags:

ADO.NET ENTITY FRAMEWORK: Simple Insert Update Delete in Entity Framework

September 4, 2012 Leave a comment

Introduction

In this blog I am going to show you how to perform simple select, insert, update and delete operation using Entity Framework.

Entity framework  support three model to work with data in your database

  1. Database First
  2. Model First
  3. Code First

Database First: In database First your database first created and then we use that created database in my operation with Entity Framework.

Other two approach/model i will be posting in my next blog.As my aim is to keep this post as simple as possible so i will  be focusing more simple CRUD operation using Database first approach.

Step 1) let’s see database schema

This is the table in database against which I am going to do the CRUD operation

Step 2) create a project in visual studio and add EDM (Entity Data Model)

As you can see I have selected ADO.NET Entity Data Model this is main item to work with Entity framework, without it it’s impossible to work with Entity Framework

Now select Generate from database (this is called database first approach). There are two other code first and model first that I will cover in my upcoming blog post.

Over here you can give the name of entity model of which you will create object that will work as entry point of EMD.

Select your table, view Store procedure over here.

Step 3) let’s see EDM added in project

Step 4) Now Final CRUD operation will be done from here

Let’s start with insert operation

using (EFDemoEntities cntx = new EFDemoEntities())

{
//INSERTION
Employee emp = new Employee()
              { EFName = "Sujeet", ELName = "Kumar", EAddress = "kolkata", EMobile = "9999999999" };
cntx.AddToEmployees(emp);
//or
//cntx.AddObject("Employees", emp);

}

Now lets see how to select

using (EFDemoEntities cntx = new EFDemoEntities())
{
var emp = (from e in cntx.Employees
where e.EFName == "Sujeet"
select e).FirstOrDefault();
if (emp != null)
{
Console.WriteLine(emp.EFName);
Console.WriteLine(emp.ELName);
Console.WriteLine(emp.EAddress);
Console.WriteLine(emp.EMobile);
}
}

Now lets see how to update

using (EFDemoEntities cntx = new EFDemoEntities())
{
var emp = (from e in cntx.Employees
where e.EFName == "Sujeet"
select e).FirstOrDefault();

if(emp !=null)

{

emp.EFName="Sujit";

emp.ELName="Kumar Singh";

emp.EAddress="Kolkata City";

emp.EMobile = "9877777777";

}

cntx.SaveChanges();

}

Now lets see how to delete

using (EFDemoEntities cntx = new EFDemoEntities())
{

var emp = (from e in cntx.Employees
where e.EFName == "Sujit"
select e).FirstOrDefault();

cntx.DeleteObject(emp);
cntx.SaveChanges();
}

I tried my best to keep it simple as much as possible so that reader can take interest in this technology and at least get started.

Hope you will learn something new from this blog.

If you like this post or this blog site , please give me feedback ad like this blog Facebook page . it motivate and make me write more and more .

If you have any problem in understanding this technology then you can ask me .

Thanks for reading this post .


%d bloggers like this: