ENTITY FRAMEWORK
Entity framework is an Object Relational Mapping framework and it helps developer to keep DB design separate from domain class design.
Entity framework automate mechanism of sorting and accessing data in DB.
EDM (Entity Data Model) -
Storage Model - DB design Model (including Tables,Views,SP,...)
Conceptual Model - Model Class
Mapping - Mapping between Storage Model and Conceptual Model
Linq to Entites -
Query Language
Entity SQL -
Query language for complex queries
Object Service -
Entry point for data access from DB
Entity Client Data Provider -
Conver Linq to Entities/Entity SQL queries to SQL
ADO.Net Data Provider -
Communicate with Database
Practical Example:-
Add-> New Item ->ADO.NET Entity Data Model (in the data sub menu)
Then give a proper name to your model and click on Add button.
If you need to generate your data model from existing database you can select ‘generate from database’ option otherwise you can select an empty model. In here I’ll use my existing database as I will select ‘generate from database’ option and select next button.
Then you will get following screen. In here you can select the connection for your database. If you need to add new connection you can go through the ‘new connection’ button. Then select next.
From the next screen you can select what tables (or views, stored procedures...) you need to add for your model. After selecting required entities you can select finish button.
Then you will have a mapping design generated from your database.
Following is my example mapping design.
Then add new C# class to your project. I’ll name it as ‘GetData.cs’. Using this class you can directly access to the database table data.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace testapp2.data
{
public class GetData
{
NORTHWINDEntities northwindEntity = new NORTHWINDEntities();
/// <summary>
/// Get all user data from userdata table.
/// </summary>
/// <returns></returns>
public IList<UserData> GetUserData()
{
var userData = (from n in northwindEntity.UserDatas
select n).ToList();
return userData;
}
/// <summary>
/// Get all customers from Customer table.
/// </summary>
/// <returns></returns>
public IList<Customer> GetCustomerData()
{
var customers = (from c in northwindEntity.Customers
select c).ToList();
return customers;
}
}
}
No comments:
Post a Comment