⬆️ ⬇️

Entity Framework Core 2.2 released. What's new? (3 of 3)

On December 4, the final version of EF Core 2.2 was released. It is released in parallel with ASP.NET Core 2.2 and .NET Core 2.2 and is the most recent release of our open-source and cross-platform technology for managing the mappings between language objects and the database.



EF Core 2.2 RTM contains more than a hundred fixes and several new features, which we will discuss in this article.



Links lead to relevant articles on Habré. This is the last, third article of the series. Next time we will talk about the new release - and it will be in the new year.





Spatial data



Spatial data is used to store the physical location and shape of objects. Many existing databases have built-in methods for storing, indexing, and searching for such data. The main use scenarios are to search for objects at a selected distance and verify that one of the polygons contains a given point. EF Core 2.2 can now work with such databases and geodata in them using types from the NetTopologySuite library (NTS).



Spatial data is implemented as a set of extension packages for special providers. Each of these packages adds both new mappings for NTS types and methods, and the corresponding spatial types and functions in the database. Such provider extensions have already been implemented for SQL Server, SQLite and PostgreSQL (thanks to the Npgsql project). Spatial types can be used directly, together with the EF Core in-memory provider, without using any other extensions.



As soon as the extension is installed, support for new types is included. Properties with these types can be used in your entities, for example:



using NetTopologySuite.Geometries; namespace MyApp { public class Friend { [Key] public string Name { get; set; } [Required] public Point Location { get; set; } } } 


Of course, now this data can be saved:



 using (var context = new MyDbContext()) { context.Add( new Friend { Name = "Bill", Location = new Point(-122.34877, 47.6233355) {SRID = 4326 } }); context.SaveChanges(); } 


Similarly, it becomes possible to make queries to the database that involve spatial data and operations:



  var nearestFriends = (from f in context.Friends orderby f.Location.Distance(myLocation) descending select f).Take(5).ToList(); 


Spatial data is a big topic, familiarity with which you should start with official documentation .



Dependent Entity Collections



In EF Core 2.0, it became possible to model one-to-one relationships. EF Core 2.2 extends this feature with the ability to directly indicate who is in this respect the main entity (owner) and who is dependent (owned). This allows you to limit and clarify the scope of the entity.



For example, dependent entities:





In relational databases, dependent collections are not displayed in the table of the main entity, but in separate tables, similar to the usual one-to-many relationships. In document-oriented databases, everything is somewhat different, and we plan to put dependent entities (in dependent collections or links) in the same document that stores the main entity.



The feature can be used by calling the new OwnsMany() API:



 modelBuilder.Entity<Customer>().OwnsMany(c => c.Addresses); 


For more information, refer to the documentation .



Query tags



This feature is designed to simplify the task of finding the connection between LINQ queries in the code and the SQL queries generated from them, which can be found in the logs.



To enable tags, you must annotate a LINQ query using the new TagWith() method. Let's slightly modify the previous example from the section on spatial data:



  var nearestFriends = (from f in context.Friends.TagWith(@"This is my spatial query!") orderby f.Location.Distance(myLocation) descending select f).Take(5).ToList(); 


In the log you will see the following text:



 -- This is my spatial query! SELECT TOP(@__p_1) [f].[Name], [f].[Location] FROM [Friends] AS [f] ORDER BY [f].[Location].STDistance(@__myLocation_0) DESC 


As always, there is about it [section in the documentation].



Compatible with EF Core 2.1



We spent a lot of time and effort by ensuring backward compatibility of EF Core 2.2 with existing providers of EF Core 2.1 and making sure that the application is assembled without any visible problems after updating to EF Core 2.2. Most likely, in most cases, the migration to the new version will be simple, but nevertheless, if you suddenly encounter problems, you should tell about them in our bugtracker .



At the moment there is only one change that may require small changes to the application code. You can read about it in the description of the following ticket:





We intend to continue to maintain and update the list of problems requiring modification of the old code.



What's next: EF Core 3.0



After the release of version 2.2, our next target is EF Core 3.0. We have not yet implemented any new features, so the NuGet packages released on December 4 contain only a few small changes made after the release of EF Core 2.2.



There are already some widely discussed big ideas planned for the next release. We want to talk about them in the following news releases, but here are a few topics about which you can already say something:





Conclusion



The EF team thanks the community for the quality feedback and development assistance that eventually led to the emergence of EF Core 2.2. As always, we remind you that new ishshui can be added directly to our tracker . Thank!



Don't forget that the DotNext tickets will go up from January 1st. Personal - for a thousand, and Standard - for two thousand. Details about Early Bird - on the site .


')

Source: https://habr.com/ru/post/433442/



All Articles