3 Using the Data Provider with the ADO.NET Entity Framework : Enhancing ADO.NET Entity Framework Performance

Enhancing ADO.NET Entity Framework Performance
Although the Entity Framework offers powerful productivity gains, some developers believe that the Entity Framework takes too much control of the features they need to optimize performance in their applications.
ADO.NET has a well-established set of relatively simple methods that can be used to enable and manage features such as connection statistics and reauthentication. DataDirect ADO.NET Entity Framework data providers include additional enhancements that can be used to enable, retrieve and reset statistical counters on a connection. Developers can use these enhancements to determine and then ultimately improve the application’s runtime performance.
The Entity Framework includes a similar set of methods that have been tailored to be useful for the new Entity Framework consumers — LINQ, EntitySQL, and ObjectServices.
This functionality is modeled in the xml file provided in Appendix A “Using an .edmx File”. By surfacing the DDTekConnectionStatistics and DDTekStatus entities, you can quickly model this code using the standard tooling.
Obtaining Connection Statistics
To obtain connection statistics, first, establish an Entity Container, DDTekConnectionContext, in which two Entity Sets, DDTekConnectionStatistics and DDTekStatus, are defined. Then, to interact with each Entity, include functions to retrieve results.
The following C# code fragment shows how you gain access to these statistics using ObjectContext:
DTekConnectionContext objCtx = new DDTekConnectionContext();
   DDTekStatus status = objCtx.DisableStatistics().First();
   MessageBox.Show("StatisticsEnabled = " + status.StatisticsEnabled);
   status = objCtx.EnableStatistics().First();
   MessageBox.Show("StatisticsEnabled = " + status.StatisticsEnabled);
   DDTekConnectionStatistics statistics = objCtx.RetrieveStatistics().First();
   MessageBox.Show("BytesReceived/Sent: " +
   statistics.BytesReceived + "/" + statistics.BytesSent);
where DDTekConnectionContext is declared in the app.config file:
<add name="DDTekConnectionContext"
connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=DDTek.Oracle;provider connection string=&quot;Host=nc-lnx02;Password=login4;Pooling=False;SID=CP31;User ID=login4;Reauthentication Enabled=true&quot;" providerName="System.Data.EntityClient" />
For more information about the data provider’s support for connection statistics, refer to the DataDirect Connect for ADO.NET User’s Guide.
Implementing Reauthentication
Typically, you can configure a connection pool to provide scalability for connections. In addition, to help minimize the number of connections required in a connection pool, you can switch the user associated with a connection to another user, a process known as reauthentication. For example, suppose you are using Kerberos authentication to authenticate users using their operating system user name and password.
To reduce the number of connections that must be created and managed, you may want to switch the user associated with a connection to multiple users using reauthentication. For example, suppose your connection pool contains a connection, Conn, which was established using the user ALLUSERS. You can have that connection service multiple users, User A, B, C, and so on, by switching the user associated with the connection Conn to User A, B, C, and so on.
For more information about the data provider’s support for reauthentication, refer to the DataDirect Connect for ADO.NET User’s Guide.
This functionality is modeled in the xml file provided in Appendix A “Using an .edmx File”. By surfacing the DDTekConnectionStatistics and DDTekStatus entities, you can quickly model this code using the standard tooling.
First, we establish an Entity Container, DDTekConnectionContext, in which we have two Entity Sets: DDTekConnectionStatistics and DDTekStatus. To interact with each Entity, include functions to retrieve results.
The following C# code fragment shows how you gain access to these statistics:
DTekConnectionContext objCtx = new DDTekConnectionContext();
try {
   MessageBox.Show("CurrentUser = " + status.CurrentUser);
   status = objCtx.Reauthenticate("login5", "login5", 600).First();
      MessageBox.Show("CurrentUser = " + status.CurrentUser);
     }
   catch (Exception ex) {
 
      MessageBox.Show(ex.ToString());
   }
where DDTekConnectionContext is declared in the app.config file.
<add name="DDTekConnectionContext"
connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=DDTek.Oracle;provider connection string=&quot;Host=nc-lnx02;Password=login4;Pooling=False;SID=CP31;User ID=login4;Reauthentication Enabled=true&quot;" providerName="System.Data.EntityClient" />
For more information about your data provider’s support for reauthentication, refer to the DataDirect Connect for ADO.NET User’s Guide.