Introduction in Entity Framework with C#

What is Entity Framework:

Entity Framework is an Object Relational Mapping (ORM) tool proposed by Microsoft that generates a Data Access layer from a database automatically. It retrieves and manipulates data as strongly typed objects.

Example of using Entity Framework: 

This example explains how to create an ADO.NET Entity Data Model, we will develop C# Windows Forms Application using Visual Studio 2010. First, let's create the Entity Data Model:
In Add new item window choose ADO.NET Data Entity Model and rename it to EmployeeModel.

Entity framework for beginner

 Then choose Generate from database:

Entity Framework for beginners

Select the DataTable and the Stored Procedure:

Entity Framework for beginners

This is the Data Entity Model created:

Entity Framework

 

Using Stored Procedure in Entity Framework:

To use Stored Procedure in Entity Framework we have to add it in the Entity Data Model Then generate a method to use it. The example below explains how to use SelectStoredProcedure to display list of employees in Employee DataTable in Datagridview:

- Right click on the Entity Data model and choose Model Browser.

Entity Framework tutorial for beginners

- In Model Browser Right click on the SelectStoredProcedure and click Add Function Import.

Entity Framework tutorial for beginners

- In 'Returns a collection of' select Entities and choose Employee from the Drop-Down menu

Entity Framework tutorial for beginners

- In DisplayButton click event add this code:

private void DisplayButton_Click(object sender, EventArgs e)
{
    Database1Entities db=new Database1Entities();
    List<Employee> emp = db.SelectStoredProcedure().ToList();
    dataGridView1.DataSource = emp;        
}

Database1Entities is the ObjectContext class generated from our database named Database1.

Reference:

Entity-Framework-Tutorial-for-Beginners - codeproject
Introduction in Entity Framework with C# Introduction in Entity Framework with C# Reviewed by Bloggeur DZ on 03:46 Rating: 5

Aucun commentaire:

Fourni par Blogger.