Enhancing 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. The ADO.NET Entity Framework data provider includes 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 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 Using an .edmx File. By surfacing the PsqlConnectionStatistics and PsqlStatus entities, you can quickly model this code using the standard tooling.
Obtaining Connection Statistics
First, establish an Entity Container, PsqlConnectionContext, in which there are two Entity Sets: PsqlConnectionStatistics and PsqlStatus. To interact with each Entity, include functions to retrieve results.
The following C# code fragment shows how to gain access to these statistics using ObjectConnect:
DTekConnectionContext objCtx = new PsqlConnectionContext();
PsqlStatus status = objCtx.DisableStatistics().First();
MessageBox.Show("StatisticsEnabled = " + status.StatisticsEnabled);
status = objCtx.EnableStatistics().First();
MessageBox.Show("StatisticsEnabled = " + status.StatisticsEnabled);
PsqlConnectionStatistics statistics = objCtx.RetrieveStatistics().First();
MessageBox.Show("BytesReceived/Sent: " +
statistics.BytesReceived + "/" + statistics.BytesSent);
where PsqlConnectionContext is declared in the app.config file.
<add name="PsqlConnectionContext"
connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=Pervasive.Data.SqlClient;provider connection string=&quot;Host=nc-lnx02;Password=login4;Pooling=False;SID=CP31;User ID=login4;Reauthentication Enabled=true&quot;" providerName="System.Data.EntityClient" />
See Obtaining Connection Statistics for more information about the data provider’s support for connection statistics.
Limiting the Size of XML Schema Files
Building large models with the Entity Data Model (EDM) can be very inefficient. For optimal results, consider breaking up a model when it has reached 50 to 100 entities.
The size of the XML schema files is to some extent proportional to the number of tables, views, or stored procedures in the database from which you generated the model. As the size of the schema files increase, additional time is needed to parse and create an in-memory model for the metadata. This is a one-time performance cost that is incurred for each ObjectContext instance.
This metadata is cached per application domain, based on the EntityConnection String. This means that if you use the same EntityConnection string in multiple ObjectContext instances in a single application domain, the application incurs the cost of loading metadata only once. However, the performance cost could still be significant if the size of the model becomes large and the application is not a long-running one.