{"id":20683,"date":"2018-12-04T08:30:21","date_gmt":"2018-12-04T16:30:21","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/dotnet\/?p=20405"},"modified":"2019-05-08T13:36:37","modified_gmt":"2019-05-08T20:36:37","slug":"announcing-entity-framework-core-2-2","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/dotnet\/announcing-entity-framework-core-2-2\/","title":{"rendered":"Announcing Entity Framework Core 2.2"},"content":{"rendered":"<p>Today we&#8217;re making the final version of EF Core 2.2 available, alongside <a href=\"https:\/\/aka.ms\/aspnetcore22announce\">ASP.NET Core 2.2<\/a> and <a href=\"https:\/\/aka.ms\/netcore22announce\">.NET Core 2.2<\/a>. This is the latest release of our open-source and cross-platform object-database mapping technology.<\/p>\n<p>EF Core 2.2 RTM includes more than a hundred bug fixes and a few new features:<\/p>\n<h2 id=\"spatial-data-support\">Spatial data support<\/h2>\n<p>Spatial data can be used to represent the physical location and shape of objects. Many databases can natively store, index, and query spatial data. Common scenarios include querying for objects within a given distance, and testing if a polygon contains a given location. EF Core 2.2 now supports working with spatial data from various databases using types from the <a href=\"https:\/\/github.com\/NetTopologySuite\/NetTopologySuite\">NetTopologySuite<\/a> (NTS) library.<\/p>\n<p>Spatial data support is implemented as a series of provider-specific extension packages. Each of these packages contributes mappings for NTS types and methods, and the corresponding spatial types and functions in the database. Such provider extensions are now available for <a href=\"https:\/\/www.nuget.org\/packages\/Microsoft.EntityFrameworkCore.SqlServer.NetTopologySuite\/\">SQL Server<\/a>, <a href=\"https:\/\/www.nuget.org\/packages\/Microsoft.EntityFrameworkCore.Sqlite.NetTopologySuite\/\">SQLite<\/a>, and <a href=\"https:\/\/www.nuget.org\/packages\/Npgsql.EntityFrameworkCore.PostgreSQL.NetTopologySuite\/\">PostgreSQL<\/a> (from the <a href=\"http:\/\/www.npgsql.org\/\">Npgsql project<\/a>). Spatial types can be used directly with the <a href=\"https:\/\/docs.microsoft.com\/en-us\/ef\/core\/providers\/in-memory\/\">EF Core in-memory provider<\/a> without additional extensions.<\/p>\n<p>Once the provider extension is installed, you can enable it in your DbContext calling the UseNetTopologySuite method. For example, using SQL Server:<\/p>\n<pre class=\"lang:default decode:true \">public class MyDbContext : DbContext\r\n{\r\n    public DbSet Friends { get; set; }\r\n\r\n    protected override void OnConfiguring(DbContextOptionsBuilder options)\r\n    {\r\n        options.UseSqlServer(\r\n            \"Server=(localdb)\\\\mssqllocaldb;Database=SpatialFriends;ConnectRetryCount=0\",\r\n            b =&gt; b.UseNetTopologySuite());\r\n\r\n    }\r\n}<\/pre>\n<p>You can then start adding properties of supported types to your entities. For example:<\/p>\n<pre class=\"lang:default decode:true csharp\">using NetTopologySuite.Geometries;\r\n\r\nnamespace MyApp\r\n{\r\n  public class Friend\r\n  {\r\n    [Key]\r\n    public string Name { get; set; }\r\n\r\n    [Required]\r\n    public Point Location { get; set; }\r\n  }\r\n}<\/pre>\n<p>You can then persist entities with spatial data:<\/p>\n<pre class=\"lang:default decode:true csharp\">using (var context = new MyDbContext())\r\n{\r\n    context.Add(\r\n        new Friend\r\n        {\r\n            Name = \"Bill\",\r\n            Location = new Point(-122.34877, 47.6233355) {SRID = 4326 }\r\n        });\r\n    context.SaveChanges();\r\n}<\/pre>\n<p>And you can execute database queries based on spatial data and operations:<\/p>\n<pre class=\"lang:default decode:true\">  var nearestFriends =\r\n      (from f in context.Friends\r\n      orderby f.Location.Distance(myLocation) descending\r\n      select f).Take(5).ToList();<\/pre>\n<p>For more information on this feature, see the <a href=\"https:\/\/docs.microsoft.com\/ef\/core\/modeling\/spatial\">spatial data documentation<\/a>.<\/p>\n<h2 id=\"collections-of-owned-entities\">Collections of owned entities<\/h2>\n<p>EF Core 2.0 added the ability to model ownership in one-to-one associations. EF Core 2.2 extends the ability to express ownership to one-to-many associations. Ownership helps constrain how entities are used.<\/p>\n<p>For example, owned entities: &#8211; Can only ever appear on navigation properties of other entity types. &#8211; Are automatically loaded, and can only be tracked by a DbContext alongside their owner.<\/p>\n<p>In relational databases, owned collections are mapped to separate tables from the owner, just like regular one-to-many associations. But in document-oriented databases, we plan to nest owned entities (in owned collections or references) within the same document as the owner.<\/p>\n<p>You can use the feature by calling the new OwnsMany() API:<\/p>\n<pre class=\"lang:default decode:true\">modelBuilder.Entity&lt;Customer&gt;().OwnsMany(c =&gt; c.Addresses);<\/pre>\n<p>For more information, see the <a href=\"https:\/\/docs.microsoft.com\/ef\/core\/modeling\/owned-entities#collections-of-owned-types\">updated owned entities documentation<\/a>.<\/p>\n<h2 id=\"query-tags\">Query tags<\/h2>\n<p>This feature simplifies the correlation of LINQ queries in code with generated SQL queries captured in logs.<\/p>\n<p>To take advantage of query tags, you annotate a LINQ query using the new TagWith() method. Using the spatial query from a previous example:<\/p>\n<pre class=\"lang:default decode:true\">  var nearestFriends =\r\n      (from f in context.Friends.TagWith(@\"This is my spatial query!\")\r\n      orderby f.Location.Distance(myLocation) descending\r\n      select f).Take(5).ToList();<\/pre>\n<p>This LINQ query will produce the following SQL output:<\/p>\n<pre class=\"lang:default decode:true\">-- This is my spatial query!\r\n\r\nSELECT TOP(@__p_1) [f].[Name], [f].[Location]\r\nFROM [Friends] AS [f]\r\nORDER BY [f].[Location].STDistance(@__myLocation_0) DESC<\/pre>\n<p>For more information, see the <a href=\"https:\/\/docs.microsoft.com\/ef\/core\/querying\/tags\">query tags documentation<\/a>.<\/p>\n<h2 id=\"getting-ef-core-2.2\">Getting EF Core 2.2<\/h2>\n<p>The EF Core NuGet packages are available <a href=\"https:\/\/www.nuget.org\/packages\/Microsoft.EntityFrameworkCore\/2.2.0\">on the NuGet Gallery<\/a>, and also as part of <a href=\"https:\/\/aka.ms\/aspnetcore22announce\">ASP.NET Core 2.2<\/a> and the <a href=\"https:\/\/dotnet.microsoft.com\/download\/dotnet-core\/2.2\">new .NET Core SDK<\/a>.<\/p>\n<p>If you want to use EF Core in an application based on ASP.NET Core, we recommend that first you upgrade your application to <a href=\"https:\/\/aka.ms\/aspnetcore22announce\">ASP.NET Core 2.2<\/a>.<\/p>\n<p>In general, the best way to use EF Core in an application is to install the corresponding NuGet package for the provider your application will use. For example, to add the 2.2 version of the SQL Server provider in a .NET Core project from the command line, use:<\/p>\n<pre class=\"lang:default decode:true\">$ dotnet add package Microsoft.EntityFrameworkCore.SqlServer -v 2.2.0<\/pre>\n<p>Or from the Package Manager Console in Visual Studio:<\/p>\n<pre class=\"lang:default decode:true  \">PM&gt; Install-Package Microsoft.EntityFrameworkCore.SqlServer -Version 2.2.0<\/pre>\n<p>For more information on how to add EF Core to your projects, see <a href=\"https:\/\/docs.microsoft.com\/ef\/core\/get-started\/install\/\">our documentation on Installing Entity Framework Core<\/a>.<\/p>\n<h2 id=\"compatibility-with-ef-core-2.1\">Compatibility with EF Core 2.1<\/h2>\n<p>We spent much time and effort making sure that EF Core 2.2 is backwards compatible with existing EF Core 2.1 providers, and that updating an application to build on EF Core 2.2 won&#8217;t cause compatibility issues. We expect most upgrades to be smooth, however if you find any unexpected issues, please report them to our <a href=\"https:\/\/github.com\/aspnet\/EntityFrameworkCore\/issues\/new\">issue tracker<\/a>.<\/p>\n<p>There is one known change in EF Core 2.2 that could require minor updates in application code. Read the description of the following issue for more details:<\/p>\n<ul>\n<li><em><a href=\"https:\/\/github.com\/aspnet\/EntityFrameworkCore\/issues\/13986\">#13986<\/a> Type configured as both owned entity and regular entity requires a primary key to be defined after upgrading from 2.1 to 2.2<\/em><\/li>\n<\/ul>\n<p>We intend to maintain <a href=\"https:\/\/github.com\/aspnet\/EntityFrameworkCore\/issues?utf8=%E2%9C%93&amp;q=is%3Aissue+label%3Aconsider-for-release-notes+milestone%3A2.2.0\">a list of issues that may require adjustments to existing code<\/a> on our issue tracker.<\/p>\n<h2 id=\"whats-next-ef-core-3.0\">What&#8217;s next: EF Core 3.0<\/h2>\n<p>With EF Core 2.2 out the door, our main focus is now EF Core 3.0.\u00a0We haven&#8217;t completed any new features yet, so the\u00a0EF Core 3.0 Preview 1 packages available <a href=\"https:\/\/www.nuget.org\/packages\/Microsoft.EntityFrameworkCore\/3.0.0-preview.18572.1\">on the NuGet Gallery<\/a>\u00a0today only contain <a href=\"https:\/\/github.com\/aspnet\/EntityFrameworkCore\/issues?q=is%3Aissue+milestone%3A3.0.0+is%3Aclosed+label%3Aclosed-fixed\">minor changes made since EF Core 2.2<\/a>.<\/p>\n<p>In fact, there are several details of the next major release still under discussion, and we plan to share more information in upcoming announcements, but here are some of the themes we know about so far:<\/p>\n<ul>\n<li><strong>LINQ improvements<\/strong>: LINQ enables you to write database queries without leaving your language of choice, taking advantage of rich type information to get IntelliSense and compile-time type checking. But LINQ also enables you to write an unlimited number of complicated queries, and that has always been a huge challenge for LINQ providers. In the first few versions of EF Core, we solved that in part by figuring out what portions of a query could be translated to SQL, and then by allowing the rest of the query to execute in memory on the client. This client-side execution can be desirable in some situations, but in many other cases it can result in inefficient queries that may not identified until an application is deployed to production. In EF Core 3.0, we are planning to make profound changes to how our LINQ implementation works, and how we test it. The goals are to make it more robust (for example, to avoid breaking queries in patch releases), to be able to translate more expressions correctly into SQL, to generate efficient queries in more cases, and to prevent inefficient queries from going undetected.<\/li>\n<li><strong>Cosmos DB support<\/strong>: We&#8217;re working on a Cosmos DB provider for EF Core, to enable developers familiar with the EF programing model to easily target Azure Cosmos DB as an application database. The goal is to make some of the advantages of Cosmos DB, like global distribution, \u201calways on\u201d availability, elastic scalability, and low latency, even more accessible to .NET developers. The provider will enable most EF Core features, like automatic change tracking, LINQ, and value conversions, against the SQL API in Cosmos DB. We started this effort before EF Core 2.2, and <a href=\"https:\/\/devblogs.microsoft.com\/dotnet\/announcing-entity-framework-core-2-2-preview-3\/\">we have made some preview versions of the provider available<\/a>. The new plan is to continue developing the provider alongside EF Core 3.0.<\/li>\n<li><strong>C# 8.0 support<\/strong>: We want our customers to take advantage some of the <a href=\"https:\/\/devblogs.microsoft.com\/dotnet\/building-c-8-0\/\">new features coming in C# 8.0<\/a> like async streams (including await for each) and nullable reference types while using EF Core.<\/li>\n<li><strong>Reverse engineering database views into query types:<\/strong> In EF Core 2.1, we added support for query types, which can represent data that can be read from the database, but cannot be updated. Query types are a great fit for mapping database views, so in EF Core 3.0, we would like to automate the creation of query types for database views.<\/li>\n<li><strong>Property bag entities<\/strong>: This feature is about enabling entities that store data in indexed properties instead of regular properties, and also about being able to use instances of the same .NET class (potentially something as simple as a <code>Dictionary&lt;string, object&gt;<\/code>) to represent different entity types in the same EF Core model. This feature is a stepping stone to support many-to-many relationships without a join entity, which is one of the most requested improvements for EF Core.<\/li>\n<li><strong>EF 6.3 on .NET Core<\/strong>: We understand that many existing applications use previous versions of EF, and that porting them to EF Core only to take advantage of .NET Core can sometimes require a significant effort. For that reason, we will be adapting the next version of EF 6 to run on .NET Core 3.0. We are doing this to facilitate porting existing applications with minimal changes. There are going to be some limitations (for example, it will require new providers, spatial support with SQL Server won&#8217;t be enabled), and there are no new features planned for EF 6.<\/li>\n<\/ul>\n<h2 id=\"thank-you\">Thank you<\/h2>\n<p>The EF team would like to thank everyone for all the community feedback and contributions that went into EF Core 2.2. Once more, you can report any new issues you find on <a href=\"https:\/\/github.com\/aspnet\/EntityFrameworkCore\/issues\/new\">our issue tracker<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Today we&#8217;re making the final version of EF Core 2.2 available, alongside ASP.NET Core 2.2 and .NET Core 2.2. This is the latest release of our open-source and cross-platform object-database mapping technology. EF Core 2.2 RTM includes more than a hundred bug fixes and a few new features: Spatial data support Spatial data can be [&hellip;]<\/p>\n","protected":false},"author":912,"featured_media":21746,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[859],"tags":[4,9,32,70,71],"class_list":["post-20683","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-entity-framework","tag-net","tag-net-core","tag-asp-net-core","tag-entity-framework","tag-entity-framework-core"],"acf":[],"blog_post_summary":"<p>Today we&#8217;re making the final version of EF Core 2.2 available, alongside ASP.NET Core 2.2 and .NET Core 2.2. This is the latest release of our open-source and cross-platform object-database mapping technology. EF Core 2.2 RTM includes more than a hundred bug fixes and a few new features: Spatial data support Spatial data can be [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/posts\/20683","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/users\/912"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/comments?post=20683"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/posts\/20683\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/media\/21746"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/media?parent=20683"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/categories?post=20683"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/tags?post=20683"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}