Entity Framework Tutorial for Beginners


A simple tutorial for beginners to learn the basics of Entity Framework. It will teach you how to perform CRUD operations using EF.n this article, we will learn the basics of Entity Framework by creating a sample application. This tip is for the programmers who have some experience in creating ASP.NET applications, but are new to Entity Framework. Here I will explain: How to create an Entity Data Model Perform CRUD operations Use Stored Procedures

There are many articles that will teach the basics of Entity Framework. But in this post, you are going to learn by creating a simple one page application.
What is Entity Framework?
Object/Relational Mapping (ORM) framework
Work with database as domain-specific objects
Retrieve and manipulate data as strongly typed objects
Create Entity Data Model

Now, let's create the entity data model. Here are the step-by-step details:

Right-click on the project name, select Add -> New Item.
Select the Data tab from left pane, then select ADO.NET Entity Data Model.
Name it as LearnEF.edmx. Click Add.
Select Generate from database in the Entity Data Model Wizard.
In the next window, set the connection properties for your database and click Next.
The next window will show you the objects in your database. Select the table Employee and the stored procedure SearchEmployee.
Click Finish. If you have completed the above steps successfully, you will see your EDM in design mode as shown below:
Insert a new record

To insert a new record:

Add an object of Employee class. e.g.
Hide Copy Code
Employee objEmp = new Employee();
Set the value of the properties like:
Hide Copy Code
objEmp.FirstName = txtFirstName.Text;
Add object to the collection in ObjecContext and call SaveChanges:
Hide Copy Code
db.Employees.AddObject(objEmp);
db.SaveChanges();


Comments

Author: sridhar thota20 Apr 2015 Member Level: Gold   Points : 5

Entity Framework CRUD Operations:

Let us take Employee class

To Add Record:
Create instance of Employee class
Employee obj=new Employee();

Add object to collection and pass Employee object as parameter for add method.
db.Employees.add(obj);

Finally use savechanges() method to update record in data base.
db.SaveChanges();

To Edit Record:
Retrieve record with LINQ Query.
var a=(from x in db.Employees where x.emp_id=id select x)
if(a!=null)
{
a.emp_city="INDIA";
db.SaveChanges();
}

To Delete Record:
var a=(from x in db.Employees where x.emp_id=id select x)
if(a!=null)
{
db.Employees.Remove(a);
db.SaveChanges();
}


regards

sridhar.
DNS Member.



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: