© Brian L. Gorman 2020
B. L. GormanPractical Entity Frameworkhttps://doi.org/10.1007/978-1-4842-6044-9_15

15. .Net 5 and Entity Framework

Brian L. Gorman1 
(1)
Jesup, IA, USA
 

In this chapter, we’re going to have a brief discussion about the latest version of the .Net ecosystem: .Net 5. This new (at the time of this book writing and publication) version is slated for release in the last quarter of 2020. As such, we need to be ready to adapt to any changes that this framework will bring us.

One framework to rule them all

One thing of note is that with the .Net 5 release, there will be no more .Net Framework, and there will be no more .Net Core. Everything will be housed in the same place, and all of the moving pieces should work together from this point on. As a .Net developer, we should be able to have a similar development experience to what we are used to, even after .Net 5 is released.

A combination of the best parts of everything

According to the official blog posts and statements about .Net 5 from Microsoft’s Richard Lander, the goals of .Net 5 will be to “improve .Net in a few key ways” (such as):
  • Produce a single .NET runtime and framework that can be used everywhere and that has uniform behaviors and developer experiences

  • Expand the capabilities of .NET by taking the best of .NET Core, .Net Framework, Xamarin and Mono.

  • Build that product out of a single code-base that developers (Microsoft and the community) can work on and expand together and that improves all scenarios.

—Lander, 2019

As you can imagine, this is a great thing for all of us. As Microsoft continues to expand into the future, we will be able to run our projects on any machine architecture or in containers, and we will be able to also develop from any machine. Additionally, it won’t matter what we are building; our development experience will be the same. In the blog post, Richard goes on to mention

This new project and direction are a game-changer for .NET. With .NET 5, your code and project files will look and feel the same, no matter which type of app you’re building. You’ll have access to the same runtime, API and language capabilities with each app. This includes new performance improvements

—Lander, 2019

(references from https://devblogs.microsoft.com/dotnet/introducing-net-5/)

EF6, EFCore, and .NET 5

With the direction of .NET 5 being the wave of the future, we need to know what that will do to our current EF6 and EFCore applications. The really good news for us is that we should be able to keep working with our solutions.

.Net Core 3.0+ gave us the ability to use an EF6 implementation. The .Net Framework also runs EF6. .NET 5 is bringing all of this together, so both EFCore and EF6 should work as expected in .NET 5.

EFCore5

As .NET 5 is coming out, the next version of Entity framework is also being released. The next version of EF is currently referred to as EFCore5. This is going to confuse a lot of people, since it would seem that EF5 has already been released prior to EF6.

Core is going away, right?

Even more confusion with this vNext name for EF might be caused since the Core moniker is slated to go away with the .NET 5 release in November.

All of this to really just say that we might see a name change of this product, or we might not. Either way, what we can rely on is that the vNext EF – EFCore5 - is out and ready to be used and will work in the .NET 5 release that happens at the end of 2020. The good news is that it also works on the latest release of EFCore. That means that even if you don’t upgrade your current production apps out of the .Net Core 3.1 release, you will likely still be able to use EFCore5.

Changes with EFCore5

I really want to talk about some of the new and useful things we can do with the vNext release – EFCore5. Likely, the things I’m talking about here won’t be the only things that you’ll get with the new version, but this is what I know about as of the time of this book being published. Additional information can be found here: https://docs.microsoft.com/en-us/ef/core/what-is-new/ef-core-5.0/plan. What follows is a summary of a few of the new features I think you’ll be glad to be aware of.

Many-to-many navigation properties

https://github.com/dotnet/efcore/issues/19003

To be honest, I can see why this was so heavily requested to be added to the new version. In a nutshell, when working with many-to-many relationships in the past, we would create the navigation properties, and then to load the data we need to use along with appropriate includes to bring the data into our query results.

Consider our relationship between Items and Genres, which takes place through the ItemGenre table. If we are writing a query to get our Genre info included when we are getting Items, we have to do something like this:
var items = _context.Items
                    .Include(x => x.ItemGenres)
                    .ThenInclude(y => y.Genre)
                    .ToList();
With the new EFCore5 MTM navigation properties, the query will be reduced to the following:
_context.Items.Where(i => i.ItemGenres
                            .Select(g => g.Name)).ToList();

Not having to directly include the join in the query will make it quite a bit more concise and will seem more readable.

Table-per-type (TPT) inheritance mapping

Table per type is a pattern that, when implemented, works exactly how it sounds. With TPT , as you build out an object hierarchy with inheritance, each new type gets its own table. Contrast this with TPH – table per hierarchy – where the entire set of objects are stored in one table.

So here is the interesting thing. TPT is already able to be implemented in EFCore 3.1. However, there are a few hoops to jump through and you end up with one table (much like TPH) that has all types in it with a simple differentiator field. As you might imagine, this can quickly become non-performant in a high-volume database. First you have to select by differentiator, and then filter the results further to get the subset of the specific type that you need for your operation. Sure, you can put indexes on things, but it still means your table grows exponentially as more types are added.

With EFCore5, we’ll get true TPT capabilities. Unfortunately, as per the documentation on the plan, this may come with some breaking changes that would require rework from an existing solution moving into EFCore5.

Filtered include

Another feature that will surely help our day-to-day operations is the ability to filter an include statement. As of now, if you try to add a filter on an include, you get an exception and the query won’t execute.

The filtered include will provide the ability to select only the subset of joined table rows that match based on the filter.

Rationalize ToTable, ToQuery, ToView, FromSql

In the operation of EF, there are different things that can happen, whether a query is executed, a migration generates a table definition change, and views exist with or without keys. All of this working together has led to a place where you really need to know the specific way it works for each operation.

It seems that the EFCore5 release will be working to try to make these operations standard for each To or From call so that the developer won’t have to know some of the internal nuances of the build or update pipeline to get the results they need.

For example, a situation might exist where specific operations like Create, Update, and Read need to happen against a physical table, but perhaps the Read comes from a procedure result or a view. In those cases, the goal would be to correctly get objects created in the migration so that the system will work as planned.

Another goal of this work is to get the mappings to other objects working in a new way that should be easier for developers, for example, trying to get the mapping to stored procedures for your Create, Update, and Delete operations, or allowing the ability to execute raw SQL for defining objects from a query.

Migrations and deployment experience

For code-first developers, this area has consistently been one where the experience is not always ideal. If you’ve worked in EF6, you know that even the date of the migration is important. Each migration needs to run in order according to the date on the migration. This leads to a lot of migration conflicts where one developer beats another developer to production, and the second developer must set their database correctly by rolling back their migration, delete their migration, get the new code, update the database, and then finish by re-creating their original migration with the new timestamp and model snapshot.

This problem is especially painful when a migration conflict happens on the production database. As such, both development and deployment have been troubled by migration conflicts. This experience is vastly improved in EFCore and will continue to be improved in EFCore5.

With EFCore5, one goal will be to create a better team experience, presumably by mitigating even more of these migration conflicts and even being smart enough to handle situations that would cause a conflict.

Another major change will be that the ability to run migrations across all platforms should exist. As a result of being able to run migrations on any platform in EFCore5, the overall experience should be especially improved for Linux and Mac users.

EFCore platforms experience

As mentioned at the start of the chapter, an overarching goal of .NET 5 will be the ability to have a similar experience on all types of projects, from Xamarin to Blazor to ASP.Net web applications.

As such, another goal of the EFCore5 release will be an improved experience working with EF for all of the major platforms, not just ASP.Net.

Performance

As EF has iterated through the years, each version has added performance improvements. EFCore definitely has improved performance over EF6 on just about every metric.

I would expect that EFCore5 will continue to improve on the performance of all operations. Additionally, the documentation points out that there is a plan for a new batching API to allow multiple statements to be batched for increased performance.

Final thoughts for this chapter

In this chapter, we talked about the future of .Net with the upcoming release of .NET 5. We also discussed the fact that EFCore5 is already available and can be used within EFCore 3.1.

Likely, by the time you are reading this book, .NET 5 will be out of preview and EFCore5 will be fully implemented.

Even in the new version, things we’ve covered in this book will remain incredibly relevant. While there may be improvements to things like View creation and interaction or mapped stored procedures for CRUD operations, the same concepts around migrations, models, and general development of code-first databases will remain.

We concluded this chapter, and ultimately this book, with a look at the new features that will be available in EFCore5. A couple of the key features are
  • Table-per-type (TPT) implementation

  • Many-to-many navigation properties

  • Filtered include statements

  • An improved migration and deployment experience

  • An overall uniform and improved experience for all development efforts regardless of platform

There will definitely be more features than we’ve talked about here implemented in EFCore5, so make sure to review the documentation to know exactly what is available to utilize in your solutions.

Conclusion

I hope you have enjoyed reading and working through this book as much as I’ve enjoyed creating it for you. Alas, the end is nigh.

Our time together doesn’t have to be over, however. Please don’t hesitate to connect with me on LinkedIn or Twitter. I would love to hear your story and get your thoughts on how this book has helped you in your day-to-day work.

I wish you all the best in your development endeavors. May you have peace, joy, and abundance in your life.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.222.120.133