I recently updated an app with LINQ to SQL and SQL Server CE 3.5 to Entity Framework 4.1 Code First and SQL Server CE 4.0, and it's now running noticeably slower. I did some before vs. after stopwatch testing, and most major operations of my app appear to be running about 40% slower on average.
I'm using all default strategies and configurations for EF Code First except for disabling cascading deletes.
When I originally posted this question, I was focused on one query that seemed to be taking particularly long, but I've since realized that it was only particularly slow on first run (see the comment thread below).
What I now think I'm seeing is that most queries are running slower--not dramatically slower, but slow enough to quickly add up as most operations the app performs involve several queries.
This app has a very small database. The SQL CE (.sdf) file is only 458 KB, and the largest table has less than 250 records.
Here's an example POCO class:
public class Target
{
public int Id { get; set; }
public int TrialDefinitionId { get; set; }
public int Number { get; set; }
public int X { get; set; }
public int Y { get; set; }
public string Phase { get; set; }
public virtual TrialDefinition TrialDefinition { get; set; }
}
All my classes follow this basic pattern (simple types + virtual properties to obtain objects linked by foreign keys). I have one class that uses an ICollection
to obtain a listing for a many-to-one relationship.
Final note: I'm using a repository pattern as a mediator, and each usage of a repository is placed in a using
block. For "get" operations, this results in entities becoming detached once I've obtained the data I need from the database.
Does anyone have any specific strategies for improving the performance of my EF Code First app? Please keep in mind that I haven't had a chance yet to read up on EF in much detail. I'm mostly just trying to migrate as quickly and painlessly as possible from LINQ to SQL to EF. The most useful answer for me would be one that consists of changing specific strategies or configurations or other settings.