AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |
Back to Blog
Entity framework vs dapper2/27/2023 ![]() ![]() Public void GetTeamRostersForSport(int sportId) Public void GetRosterByTeamID(int teamId) New SportContextEfCore(Database.GetOptions())) ![]() The test setup for EF Core without tracking is so similar that I'm just going to put it here: public class EntityFrameworkCore : ITestSignature The only real difference between this query and the prior one is that this one returns a list of Teams, not just a single one. Var players = (x => x.Players).Where(x => x.SportId = sportId).ToList() This will definitely make this query slower than the simple Player by ID query, but it remains to be seen exactly how much slower.įinally, let's see the code for the Rosters by Sport ID query: public void GetTeamRostersForSport(int sportId) Include() method which brings in the related Players objects for the given Team. Var teamRoster = (x => x.Players).Single(x => x.Id = teamId) Let's also see the code for the Roster by Team ID query: public void GetRosterByTeamID(int teamId) In theory, this should be very fast, and it's about as simple as this kind of query can get. Using (SportContextEfCore context = new SportContextEfCore(Database.GetOptions())) Check out the sample project for the full code.ĮF Core's claim to fame is the ease with which we can write queries, so let's take a look at the test setup for it first.įirst, here's the code for the Player by ID query: public void GetPlayerByID(int id) Note: Code samples in this post have been simplified from their true content to make them more readable. The goal with these queries is to see how Dapper and EF Core handle dealing with various amounts of data. All Rosters by Sport ID: Gets a single Sport along with all Teams and every Player for each Team in that Sport.Īs you can see, the queries get progressively more difficult, and each requires more data returned than the last.Roster by Team ID: Gets a single Team and all related Player objects.Player by ID: Get a single Player object by their ID.I decided to run simple tests on three different kinds of queries: The example project has an entire section for generating sample Sports, Teams, and Players feel free to take a look. I also needed some sample data to test against. In other words: a Sport has many Teams, and a Team has many Players. Our test setup will use the following schema: With all that in mind, let's get started building our test! Database Schema Supposedly this method greatly improves performance on EF queries our test shall treat this separately from "basic" EF Core queries. But, as I have said before, performance doesn't matter unless you can prove that it matters, and for most apps the difference in speed between EF Core and Dapper will probably not be noticeable to the average user.īecause it appears to be common knowledge that EF Core is "slow", one of the most common pieces of advice I have heard when trying to speed up Entity Framework queries is to use a little method called. These features make this library a bit more cumbersome, and possibly a lot less performant than Dapper. Thus, it's really a very thin layer between your application and the database you are using.ĮF Core, on the other hand, is a full-fledged ORM with lots of cool features, including change tracking. ![]() ![]() NET world, despite the fact that Dapper isn't really an ORM at all it's more of a mapper library designed to map data results to C# objects. Dapper and EF Core are two of the primary ORMs in use in the. ![]()
0 Comments
Read More
Leave a Reply. |