Search This Blog

2009-06-12

C# 3.0 Tutorial -9:Linq To Entities

Most applications are currently written on top of relational databases. At some point, these applications will have to interact with the data represented in a relational form. Database schemas are not always ideal for building applications, and the conceptual models of applications differ from the logical models of databases. The Entity Data Model (EDM) is a conceptual data model that can be used to model the data of a particular domain so that applications can interact with data as entities or objects.The following diagram shows the relationship between LINQ to Entities(L2E) and the Entity Framework, ADO.NET 2.0, and the data source.



It is basically the replacement of L2S(Linq to SQL).
This guide should be a good starting point for anyone whether or not they are familiar with L2S, and a quick 'jumping guide' to L2S developers. Here's how to get started:

Step 1.Create a Database in your SQL Server
Open the Sql Server ,Right Click On the Database and click on the new Database.

In the "New Database" modal window ,put the name "MyDb" in "Database Name" textbox



Step 2.Create Tables in your Database

Expend your "MyDb" Database ,Right Click on Tables and click on "New Table..."

Create 2 feilds :
a)CustomerID- the datatype is int and it is an identity column and make it is as Primary Key
b)CustomerName-the data type is varchar(50)



Save the table is "Cust_Master"

Create another table Cust_Dtls(Contain CustomerID(int) and CustLocation(varchar(50)) as follows


Make a foreign Key relation between two table with CustomerID


Step 3.Create a new Website in Visual Studio

Open Visual Studio 2008 IDE-Click on File-Click on New Website
In your New Web Site Dialog box select ASP.NET Web Site nad Choose "Visual C#" in your Language Dropdown.
Type "L2EDemo" as your Web Site name and click OK.


Step 4.Create App_Code Folder within your solution

Right Click on your Solution in the solution Explorer choose "App_Code" under the "Add Asp.NET Folder"



Step 5.Make your Ado.NET Entity Data Model
Right Click on your App_Code folder and click on "Add New Item..."
Choose "ADO.NET Entity Data Model" and name it (I left the default Model.edmx for the example)
Click on Add.



Click on Generate From Database



Click Next.
In the Entity Data Model Wizard Click on "New Connection"



Choose Microsoft SQL Server in your "Choose Data Source" Dialog Box



Choose The server Name and select your MyDb Database


Click OK and then Click on "Next"

Choose Cust_Dtls and Cust_Master tables


Then Click on Finish

Now your Model.edmx will be looked like as follows


Step 6.Access Your edmx

Go to your Default.aspx.cs Page
Include the namespace MyDbModel

using MyDbModel;

you will get the namespace from your ModelBrowser when the Model.edmx is opened


Step 7.Code for Save the record using L2E
protected void SaveRecord()
{
MyDbEntities db = new MyDbEntities();
Cust_Master cust = new Cust_Master();
cust.CustomerName = "Manab";
db.AddToCust_Master(cust);
db.SaveChanges();
}

Step 8.Code for modify records using L2E
MyDbEntities db = new MyDbEntities();
db.Cust_Master.First(c => c.CustomerID == 1).CustomerName = "Ranjan";
db.SaveChanges();

Step 9.Code to Display Records

MyDbEntities db = new MyDbEntities();
foreach (Cust_Master c in (from _c in db.Cust_Master select _c))
{
Response.Write("Customer ID : " + c.CustomerID);
Response.Write("<br/>");
Response.Write("Customer Name : " + c.CustomerName);
}

Step 10.Code To Delete Records
MyDbEntities db = new MyDbEntities();
db.DeleteObject(db.Cust_Master.First(c => c.CustomerID == 1));
db.SaveChanges();

Complete Code:

using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using MyDbModel;

public partial class _Default : System.Web.UI.Page
{
MyDbEntities db;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void SaveRecord()
{
db = new MyDbEntities();
Cust_Master cust = new Cust_Master();
cust.CustomerName = "Manab";
db.AddToCust_Master(cust);
db.SaveChanges();
}
protected void ModifyRecord()
{
db = new MyDbEntities();
db.Cust_Master.First(c => c.CustomerID == 1).CustomerName = "Ranjan";
db.SaveChanges();
}
protected void showRecords()
{
db = new MyDbEntities();
foreach (Cust_Master c in (from _c in db.Cust_Master select _c))
{
Response.Write("Customer ID : " + c.CustomerID);
Response.Write("<br/>");
Response.Write("Customer Name : " + c.CustomerName);
}
}
protected void deleteRecord()
{
db = new MyDbEntities();
db.DeleteObject(db.Cust_Master.First(c => c.CustomerID == 1));
db.SaveChanges();
}
}

Now that should get you started. Like I said, if you are familiar with L2S, this transition should be no problem, and if you are new to this whole Linq arena, this should be simple to pick up. This new ADO is getting more and more impressive the more MS works on it. I can't even imagine going back to the old methods...

Related Resource:

C# 3.0 Tutorial -1:Var Keyword
C# 3.0 Tutorial -2:Extension Methods
C# 3.0 Tutorial -3:Lambda ExpressionsC# 3.0 Tutorial -4:Object Initializers
C# 3.0 Tutorial -5:Anonymous Types
C# 3.0 Tutorial -6:Type Equivalence
C# 3.0 Tutorial -7:Projections
C# 3.0 Tutorial -8:Linq

tags:Linq to Entities tutorials ,step by step Linq to Entities(L2E)

1 comment:

Varun Sharma said...

very useful article.. it really helped me alot ....