ASP.NET
  Home arrow ASP.NET arrow Page 4 - Introduction to the ADO.NET Entity Framewo...
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
ASP.NET

Introduction to the ADO.NET Entity Framework using ASP.NET
By: Jagadish Chaterjee
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 3
    2009-04-09

    Table of Contents:
  • Introduction to the ADO.NET Entity Framework using ASP.NET
  • CRUD operations using ADO.NET Entity Framework: Screen Design
  • CRUD operations using ADO.NET Entity Framework: Source Code
  • Digging a bit more into the ADO.NET Entity Framework
  • What are Entity SQL queries?

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Introduction to the ADO.NET Entity Framework using ASP.NET - Digging a bit more into the ADO.NET Entity Framework


    (Page 4 of 5 )

     

    In the previous section, navigational properties are fetched using the "Include" method as follows: 

    Protected Sub btnSearch_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSearch.Click

    Dim ctxt As New NorthwindEntities

    Dim oEmp As Emp = (From o In ctxt.Emp.Include("Dept") _

    Where o.Empno = Me.txtEmpno.Text _

    Select o).FirstOrDefault

    If oEmp Is Nothing Then

    btnClear_Click(Nothing, Nothing)

    Me.lblMsg.Text = "Employee not found."

    Exit Sub

    End If

     

    Me.txtEname.Text = oEmp.Ename

    Me.txtSal.Text = oEmp.Sal

    Me.txtDeptno.Text = oEmp.Dept.Deptno

     

    End Sub

    When we use the "Include" option, we call it "Eager Loading." The above code can be rewritten as follows: 

    Protected Sub btnSearch_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSearch.Click

    Dim ctxt As New NorthwindEntities

    Dim oEmp As Emp = (From o In ctxt.Emp _

    Where o.Empno = Me.txtEmpno.Text _

    Select o).FirstOrDefault

    If oEmp Is Nothing Then

    Me.lblMsg.Text = String.Format("Employee with Empno: {0} not found.", Me.txtEmpno.Text)

    btnClear_Click(Nothing, Nothing)

    Exit Sub

    End If

     

    Me.txtEname.Text = oEmp.Ename

    Me.txtSal.Text = oEmp.Sal

    If Not oEmp.DeptReference.IsLoaded Then oEmp.DeptReference.Load()

    Me.txtDeptno.Text = oEmp.Dept.Deptno

     

    End Sub

    In the above code, we are explicitly executing the "Load" method to retrieve the relationship details. This is called "Explicit Loading." 

    Similarly, the add method can be rewritten as follows: 

    Protected Sub btnAdd_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnAdd.Click

    Dim ctxt As New NorthwindEntities

    Dim oDept As Dept = ctxt.Dept.Where(Function(p) p.Deptno = Me.txtDeptno.Text).FirstOrDefault

    If oDept Is Nothing Then

    Me.lblMsg.Text = "Invalid Deptno"

    Exit Sub

    End If

     

    Dim oEmp As New Emp With {.Empno = Me.txtEmpno.Text, .Ename = Me.txtEname.Text, .Sal = Me.txtSal.Text, .Dept = oDept}

    ctxt.AddToEmp(oEmp)

    ctxt.SaveChanges()

     

    Me.lblMsg.Text = "Added Successfully!"

     

    End Sub

    The above is bit different from the previously-provided code. In the above code, there is performance overhead as it retrieves department details from the database (another round trip to the database) even before adding the row to the table. 

    Another important operation when dealing with database interactions is the transaction. The ADO.NET Entity Framework supports transactions in a very simple manner. The following is the code which executes a transaction: 

    Protected Sub btnDoTrans_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnDoTrans.Click

    Try

    Dim ctxt As New NorthwindEntities

    Dim oDept As New Dept With {.Deptno = 90, .Dname = "Marketing"}

    Dim oEmp1 As New Emp With {.Empno = 9001, .Ename = "Jagadish", .Sal = 7000, .Dept = oDept}

    Dim oEmp2 As New Emp With {.Empno = 9002, .Ename = "Pulakhandam", .Sal = 5900, .Dept = oDept}

    oDept.Emp.Add(oEmp1)

    oDept.Emp.Add(oEmp2)

    ctxt.AddToDept(oDept)

    ctxt.SaveChanges()

    Me.lblMsg.Text = "Saved successfully"

    Catch ex As Exception

    If ex.Message.ToLower.Contains("innerexception") Then

    Me.lblMsg.Text = ex.InnerException.Message

    Else

    Me.lblMsg.Text = ex.Message

    End If

    End Try

    End Sub

     

    You can also observe the Events in SQL Profiler during transaction execution as follows:

     

    More ASP.NET Articles
    More By Jagadish Chaterjee


       · Hai,This is my first article on dealing with ADO.NET Entity framework with...
       · Thanks man it reduced me lot of lines of code.
     

    ASP.NET ARTICLES

    - More Advanced ASP.NET 3.5 Functions and Subr...
    - ASP.NET 3.5 Functions and Subroutines
    - Coding an IQ Test with Conditionally Driven ...
    - Developing Conditionally Driven Event Handle...
    - ASP.NET 3.5 Debugging Using Visual Web Devel...
    - Understanding Event Handlers in ASP.NET 3.5
    - Building a Web Form in ASP.NET and PHP: a Co...
    - Inserting Data into a Microsoft SQL 2008 Dat...
    - Creating an ASP.NET Dynamic Web Page Using M...
    - Retrieving Data from Microsoft SQL Server 20...
    - Building ASP.NET Web Forms to Use a MySQL Da...
    - Creating an ASP.NET Database using MS SQL 20...
    - Building an ASP.NET Website Using Include Ta...
    - Create ASP.NET Web Forms to Use a Microsoft ...
    - Editing Web Design Layout in Visual Web Deve...





    © 2003-2010 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek