Plan your next move to Entity Framework Core 5 in 15 minutes

Plan your next move to Entity Framework Core 5 in 15 minutes

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?

  1. 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

No alt text provided for this image

generated SQL:

No alt text provided for this image

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.

No alt text provided for this image
No alt text provided for this image

3. Event Counters

You can now check EF performance metrics by the new event counters tool which can be run with this command:

No alt text provided for this image

which return a bunch of different performance metrics for you to review your application performance with EF

No alt text provided for this image

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:

No alt text provided for this image

and you can define the interceptor events inheriting from SaveChangesInterceptor

No alt text provided for this image

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.

No alt text provided for this image

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:

No alt text provided for this image

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:

No alt text provided for this image

But now we can map these CLR objects to Table-per-type mapping by using the [Table("Name") ] attribute like this:

No alt text provided for this image

generated SQL as separate tables connected with foreign keys:

No alt text provided for this image

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:

No alt text provided for this image

above query generates this SQL:

No alt text provided for this image

You can split the query to generate more performant queries:

No alt text provided for this image

with SQL:

No alt text provided for this image

9. Index Attribute

You can now add indexes directly from you EF on your entities and migrations will do the magic for you:

No alt text provided for this image
No alt text provided for this image

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.

No alt text provided for this image

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


Waqar Ul Islam

Data Engineer | Software Engineer | UniMelb

4 年

Helpful! This will

回复
Saddam Akhtar

Senior DevOps Engineer at NETSOL Technologies Inc.

4 年

Very useful

要查看或添加评论,请登录

社区洞察

其他会员也浏览了