Plan your next move to Entity Framework Core 5 in 15 minutes
Zeeshan Adil
Software Engineer | Ex UN | Expert Vetted (Top 1%) @ Upwork | Helping 20k+ @ Dev Weekends | .NET | Azure | GoLang | AWS | React | Flutter | DevOps (Docker/Kubernetes/Terraform/Helm/Kafka) | Solution Design | Productivity
It's been a while since we've been developing applications with Entity Framework Core which has helped us improve the speed of development without having to spend a lot of time in data migrating and schema generation and have saved businesses tons on their time and cost.
While EF has helped us a lot with the speed of development yet It has not been very promising when it comes to performance and control over your data in past but the good news is things are changing and EF team has made huge improvements in their RC1 release of EC Core 5 and we have already started using it on some our production scale applications.
I will try to cover the maximum things that will help developers try the new version of EF in their new projects.
How to get started with EF Core 5 installation?
Run the following commands in your package manager console or PowerShell and Boom you have your shiny EF core 5 running in your project.
dotnet add package Microsoft.EntityFrameworkCore.SqlServer --version 5.0.0-rc.2.20475.6
dotnet add package Microsoft.EntityFrameworkCore --version 5.0.0-rc.2.20475.6
dotnet add package Microsoft.EntityFrameworkCore.Tools --version 5.0.0-rc.2.20475.6
dotnet add package Microsoft.EntityFrameworkCore.Design --version 5.0.0-rc.2.20475.6
Note: before you start looking at the examples and need to know how to generate the SQL for the EF queries you can just run .ToQueryString() on any of your IQueryable objects.
What's new?
- Many to Many
EF Core 5 now supports many to many relationships you can now have your entities generating many to many tables in your DB
generated SQL:
2. Mapping queries and entity types
You can now map your queries to the entity types using ToSqlQuery, just like we used to do in dapper, now we have the options to map tables, views and queries to the entity types.
3. Event Counters
You can now check EF performance metrics by the new event counters tool which can be run with this command:
which return a bunch of different performance metrics for you to review your application performance with EF
4. Interception events
You can now subscribe to your save changes events to do any kind of logging or auditing of your data by subscribing to the DB context save changes event which gives you more control over your data life cycle.
For example:
and you can define the interceptor events inheriting from SaveChangesInterceptor
5. Exclude tables from Migrations
This is a great feature if you are using multiple DB context while adhering to bounded contexts practices having the same database behind the scenes, We do it every day where we at least have two contexts one working for the identity and the second working for the database operations.
And having two separate contexts and managing their migrations have always been trouble but the EF core team has a solution and you can now exclude a table from migrations by specifying that in OnModelCreating.
6. EF Migration Improvements:
Migration scripts now run in a transaction so more security over your migrations scripts and you can also see the pending migrations using dotnet ef migrations list
7: Table-per-type mapping
If you look at these C# types being inherited from each other:
By default EF core mapping for the inherited Entity types to table-per-hierarchy mappings , which means it is mapped to ONE table, like this:
But now we can map these CLR objects to Table-per-type mapping by using the [Table("Name") ] attribute like this:
generated SQL as separate tables connected with foreign keys:
8.Split queries with Include
If you think a filtered include is not performing well in terms of performance and is not correcting correct SQL for example:
above query generates this SQL:
You can split the query to generate more performant queries:
with SQL:
9. Index Attribute
You can now add indexes directly from you EF on your entities and migrations will do the magic for you:
10. Filtered Included
Guess what? No need to pay for this feature or use any extensions you can now do filtering on your includes directly within EF.
You can also run .Take(), .Skip() or .OrderBy() features
11 DB scaffolding revisited:
No trouble with having your connection string added to your context:
dotnet ef dbcontext scaffold "connection string" Microsoft.EntityFrameworkCore.SqlServer --context-namespace "My.Context" --namespace "My.Model"
That's it. That was your 15 minutes guide to motivate you to move to EF Core 5 for more performance improvements and development speed.
12 Savepoints
In addition to all other great features, you can now save your transaction state in a DB transactional environment where in one UOW you might need to save a state of the transaction, release it or rollback it so that if something fails EF rolls back to the last savepoint and then SaveChanges() can be retired, to create the save point use the following command.
context.Database.CreateSavepoint("MySavePoint");
I hope I was able to cover the maximum features that can motivate and empower more developers to take benefit from the version of EF core and make our apps and lives easier. Happy development with EF CORE!!!!
More readings:
https://docs.microsoft.com/en-us/ef/core/what-is-new/ef-core-5.0/whatsnew
Code credits:
docs.microsoft.com
Data Engineer | Software Engineer | UniMelb
4 年Helpful! This will
Senior DevOps Engineer at NETSOL Technologies Inc.
4 年Very useful