Monday, March 25, 2013

Entity Framework Basic - Sample Implementation - Part 1



Work With Entity Data Model
   
1.     What is Entity Framework ? The  Architecture of EF.
2.     Sample Implementations(CRUD) of EDM in VS 2010.[Only With SP in Sql Server 2k8]
Code Snippet for Fill Grid & Dropdown List.
S.N Sample Code Snippets are in Italic Style.

1.What is Entity Framework ? The  Architecture of EF.
The most simplest way to describe Entity Frame Work is , it’s an ORM Tool.
An ORM Tool is one which is storing the data from Domain Objects to Relational Database objects.It can be consisting of Three sub-parts , Domain Class Objects , Relational DB objects & Mapping Information that lies between Domain Class Object & Relational DB Objects.
That is , we build a Conceptual Schema Layer that consisting DN entities like Tables , Views 7 SP’s as per our requirement.
LINQ almost do the same thing but EF has better advantages than LINQ , such as ,
A..It supports Oracle & other DB tools apart from Sql Server.
B. It maintain Reference among the Enitites based on Navigation Properties and having enough intelligence to automatically adding mapping Or rereferencing information among the tables in Relational database.
From a layman point of view, it’s an enhancement over the traditional ADO.NET way to focus the Developer much more on Business logic rather than Db transactional code snippets.


Ref: MSDN

As the Above Framework Defined it clearly shown that EF actually wrapping the ADO.Net mechanism.



2. Sample Implementations(CRUD) of EDM in VS 2010.[With SP in Sql Server 2k8]
We will use EDM in VS 2010 with Sql 2k8 for CRUD (Create, Read, Update, Delete) operations where we are simply skipped any Code level Query by using EntitySql Or LinqToEntity , rather , we are perform all our business logic in SP & consume thoses SP’s in code by using EDM.

Initially , we must create the EDM as steps mentioned below:
a.     Click at “Add new Item” in your VS Project within Solution Explorer.
b.     Add an ADO.Net Entity Data Model & rename it as SampleModel.edmx.
c.      Choose “Generate From Database” option.
d.     Provide the Database details , connection properties & a name for Entity Connection Setting. In our Sample Below it’s defined as “CheckDBEntities”
e.     Select Database objects as Tables, Views, SP’s.
f.       Put Model Namespace.
Once EDM has been build , The Entities can be associated with each others as per defined relationship for getting Navigation Properties.
Now Your EDM has been ready to consume.
READ Data:
We can get Records from Db by using SP with the help of EDM in any one of Two Ways as below:
 i.By Mapping Function With SP in EDM
ii. By Using “ExecuteStoreQuery”.
 There are two Tables  “tblUser “ & “tblFacility” as below in DB end.
UserID
Name
Contact
FacilityID
PBS
Sumanta
992233554
F-01

FacilityID
FacilityName
F-01
Noida

Now , there are Two SP’s in DB to retrieve different set of data.
create proc getuser
as
begin
 select * from tbluser
end
AND
Create proc getdata
@userid varchar(50)
As
Begin
  Select t1.userid,t1.name,t2.facilityname from tbluser t1 left join tblfacility t2 on t1.facilityid = t2.facilityid where t1.userid = @userid
End

Now , we add a different class “BL.cs” as Business Object that directly handles EDM rather in code behind page.

 

No comments:

Post a Comment