{"id":19655,"date":"2018-10-17T13:10:48","date_gmt":"2018-10-17T20:10:48","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/dotnet\/?p=19655"},"modified":"2019-04-18T17:36:36","modified_gmt":"2019-04-19T00:36:36","slug":"announcing-entity-framework-core-2-2-preview-3","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/dotnet\/announcing-entity-framework-core-2-2-preview-3\/","title":{"rendered":"Announcing Entity Framework Core 2.2 Preview 3"},"content":{"rendered":"<p>Today we are making EF Core 2.2 Preview 3 available, together with a new preview of our data provider for Cosmos DB and updated spatial extensions for various providers.<\/p>\n<p>Preview 3 is going to be the last milestone before EF Core 2.2 RTM, so now is your last chance to try the bits and give us feedback if you want to have an impact on the quality and the shape of the APIs in this release.<\/p>\n<p>Besides the new features, you can help by trying EF Core 2.2 preview 3 on applications that are using third party providers.\u00a0 Although we now have our own testing for this, there might be unforeseen compatibility problems, and the earlier we can detect them, the higher chances we have of addressing them before RTM.<\/p>\n<p>We thank you in advance for reporting any issues your find on <a href=\"https:\/\/github.com\/aspnet\/EntityFrameworkCore\/issues\/new\">our issue tracker on GitHub<\/a>.<\/p>\n<h2 id=\"ef-core-2-2-roadmap-update\">EF Core 2.2 roadmap update<\/h2>\n<p>EF Core 2.2 RTM is still planned for the end of the 2018 calendar year, alongside ASP.NET Core 2.2 and .NET Core 2.2.<\/p>\n<p>However, based on a reassessment of the progress we have made so far, and on new information about the work we need to complete 2.2, we are no longer trying to include the following features in the EF Core 2.2 RTM:<\/p>\n<ul>\n<li><strong>Reverse engineering database views into query types:<\/strong> <a href=\"https:\/\/github.com\/aspnet\/EntityFrameworkCore\/issues\/1679\">This feature<\/a> is postponed to EF Core 3.0.<\/li>\n<li><strong>Cosmos DB Provider:<\/strong> Although we have made a lot of progress setting up the required infrastructure for document-oriented database support in EF Core, and have been steadily adding functionality to the provider, realistically we cannot arrive to a state in which we can release the provider with adequate functionality and quality in the current time frame for 2.2.Overall, we have found that the work necessary to complete the provider to be more than we initially estimated. Also, ongoing evolution in Cosmos DB is leading us to frequently revisit decisions about such things as how we use the Cosmos DB SDK, whether we map all entities to a single collection by default, etc.We plan to maintain the focus on the provider and to continue working with the Cosmos DB team and to keep releasing previews of the provider regularly. You can expect at least one more preview by the end of this year, and RTM sometime in 2019. We haven&#8217;t decided yet if the Cosmos DB provider will release as part of EF Core 3.0 or earlier.A good way to keep track of our progress is <a href=\"https:\/\/github.com\/aspnet\/EntityFrameworkCore\/issues\/12086\">this checklist in our issue tracker<\/a>.<\/li>\n<\/ul>\n<h2 id=\"obtaining-the-preview\">Obtaining the preview<\/h2>\n<p>The preview bits are available <a href=\"https:\/\/www.nuget.org\/packages\/Microsoft.EntityFrameworkCore\/2.2.0-preview3-35497\">on NuGet<\/a>, and also as part of <a href=\"https:\/\/blogs.msdn.microsoft.com\/webdev\/2018\/10\/17\/asp-net-core-2-2-0-preview3-now-available\/\">ASP.NET Core 2.2 Preview 3<\/a> and the <a href=\"https:\/\/github.com\/dotnet\/core\/blob\/master\/release-notes\/2.2\/preview\/2.2.0-preview3.md\">.NET Core SDK 2.2 Preview 3<\/a>, also releasing today. If you are want to try the preview in an application based on ASP.NET Core, we recommend you follow the instructions to upgrade to ASP.NET Core 2.2 Preview 3.<\/p>\n<p>The SQL Server and in-memory providers are also included in ASP.NET Core, but for other providers and any other type of application, you will need to install the corresponding NuGet package.<\/p>\n<p>For example, to add the 2.2 Preview 3 version of the SQL Server provider in a .NET Core library or application from the command line, use:<\/p>\n<pre class=\"editor-colors lang-console\">$ dotnet add package Microsoft.EntityFrameworkCore.SqlServer -v 2.2.0-preview3-35497<\/pre>\n<p>Or from the Package Manager Console in Visual Studio:<\/p>\n<pre class=\"lang:default decode:true editor-colors lang-console\">PM&gt; Install-Package Microsoft.EntityFrameworkCore.SqlServer -Version 2.2.0-preview3-35497<\/pre>\n<p>For more details 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<p>The Cosmos DB provider and the spatial extensions ship as new separate NuGet packages. We&#8217;ll explain how to get started with them in the corresponding feature descriptions.<\/p>\n<h2 id=\"what-is-new-in-this-preview-\">What is new in this preview?<\/h2>\n<p>Around <a href=\"https:\/\/github.com\/aspnet\/EntityFrameworkCore\/issues?q=is%3Aissue+milestone%3A2.2.0-preview3+is%3Aclosed+label%3Aclosed-fixed\">69 issues have been fixed<\/a>\u00a0since we finished <a href=\"https:\/\/blogs.msdn.microsoft.com\/dotnet\/2018\/09\/12\/announcing-entity-framework-core-2-2-preview-2\/\">Preview 2 last month<\/a>. This includes product bug fixes and improvements to the new features. Specifically about the new features, the most significant changes are:<\/p>\n<h3 id=\"spatial-extensions\">Spatial extensions<\/h3>\n<ul>\n<li>We have enabled spatial extensions to work with the SQLite provider using the popular <a href=\"https:\/\/www.gaia-gis.it\/fossil\/libspatialite\/index\">SpatiaLite<\/a> library.<\/li>\n<li>We switched the default mapping of spatial properties on SQL Server from geometry to geography columns.<\/li>\n<li>In order to use spatial extensions correctly with preview 3, it is recommended that you use the GeometryFactory provided by NetTopologySuite instead of creating new instances directly.<\/li>\n<li>We collaborated with the NetTopologySuite team to create <strong>NetTopologySuite.IO.SqlServerBytes<\/strong> \u2014 a new IO module that targets .NET Standard and works directly with the SQL Server serialization format.<\/li>\n<li>We enabled reverse engineering for databases containing spatial columns. Just make sure you add the spatial extension package for your database provider before you run <strong>Scaffold-DbContext<\/strong>\u00a0or <strong>dotnet ef dbcontext scaffold<\/strong>.<\/li>\n<\/ul>\n<p>Here is an updated usage example:<\/p>\n<pre><code class=\"csharp language-csharp\">\r\n\/\/ Model class\r\npublic class Friend\r\n{\r\n  [Key]\r\n  public string Name { get; set; }\r\n\r\n  [Required]\r\n  public IPoint Location { get; set; }\r\n}\r\n\r\n\/\/ Program\r\nprivate static void Main(string[] args)\r\n{\r\n     \/\/ Create spatial factory\r\n     var geometryFactory = NtsGeometryServices.Instance.CreateGeometryFactory(srid: 4326);\r\n\r\n     \/\/ Setup data in datbase\r\n     using (var context = new MyDbContext())\r\n     {\r\n         context.Database.EnsureDeleted();\r\n         context.Database.EnsureCreated();\r\n\r\n         context.Add(\r\n             new Friend\r\n             {\r\n                 Name = \"Bill\",\r\n                 Location = geometryFactory.CreatePoint(new Coordinate(-122.34877, 47.6233355))\r\n             });\r\n         context.Add(\r\n             new Friend\r\n             {\r\n                 Name = \"Paul\",\r\n                 Location = geometryFactory.CreatePoint(new Coordinate(-122.3308366, 47.5978429))\r\n             });\r\n         context.SaveChanges();\r\n     }\r\n\r\n     \/\/ find nearest friends\r\n     using (var context = new MyDbContext())\r\n     {\r\n         var myLocation = geometryFactory.CreatePoint(new Coordinate(-122.13345, 47.6418066));\r\n\r\n         var nearestFriends =\r\n             (from f in context.Friends\r\n              orderby f.Location.Distance(myLocation) descending\r\n              select f).Take(5);\r\n\r\n         Console.WriteLine(\"Your nearest friends are:\");\r\n         foreach (var friend in nearestFriends)\r\n         {\r\n             Console.WriteLine($\"Name: {friend.Name}.\");\r\n         }\r\n     }\r\n}\r\n\r\n<\/code><\/pre>\n<p>In order to use this code with SQL Server, simply install the 2.2 preview 3 version of the <strong>Microsoft.EntityFrameworkCore.SqlServer.NetTopologySuite <\/strong>NuGet package, and configure your <strong>DbContext<\/strong> as follows:<\/p>\n<pre><code class=\"csharp language-csharp\">\r\npublic 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}\r\n<\/code><\/pre>\n<p>In order to use this code with SQLite, you can install the 2.2 preview 3 version of the <strong>Microsoft.EntityFrameworkCore.Sqlite.NetTopologySuite<\/strong> and\u00a0<strong>Microsoft.EntityFrameworkCore.Sqlite<\/strong>\u00a0packages. Then you can configure the <strong>DbContext<\/strong> like this:<\/p>\n<pre><code class=\"csharp language-csharp\">\r\npublic 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.UseSqlite(\r\n            \"Filename=SpatialFriends.db\",\r\n            x =&gt; x.UseNetTopologySuite());\r\n    }\r\n\r\n    protected override void OnModelCreating(ModelBuilder modelBuilder)\r\n    {\r\n        \/\/ For SQLite, you need to configure reference system on column\r\n        modelBuilder\r\n            .Entity()\r\n            .Property(f =&gt; f.Location)\r\n            .ForSqliteHasSrid(4326);\r\n    }\r\n}\r\n<\/code><\/pre>\n<p>Note that the spatial extension for SQLite requires the SpatiaLite library. This will be added as a dependency by the NuGet packages previously mentioned if you are on Windows, but on other systems you will need extra steps. For example:<\/p>\n<ul>\n<li>On MacOS:\n<pre class=\"editor-colors lang-console\">$ brew install libspatialite<\/pre>\n<\/li>\n<li>On Ubuntu or Debian Linux:\n<pre class=\"editor-colors lang-console\">$ apt-get install libsqlite3-mod-spatialite<\/pre>\n<\/li>\n<\/ul>\n<p>In order to use this code with the in-memory provider, simply install the <strong>NetTopologySuite\u00a0<\/strong>package, and the 2.2 preview 3 version of the <strong>Microsoft.EntityFrameworkCore.InMemory<\/strong> package. Then you can simply configure the <strong>DbContext<\/strong> like this:<\/p>\n<pre><code class=\"csharp language-csharp\">\r\npublic 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.UseInMemoryDatabase(\"SpatialFriends\");\r\n    }\r\n}\r\n<\/code><\/pre>\n<h3 id=\"cosmos-db-provider\">Cosmos DB provider<\/h3>\n<p>We have made several changes and improvements since preview 2:<\/p>\n<ul>\n<li>The package name has been renamed to Microsoft.EntityFrameworkCore.Cosmos<\/li>\n<li>The <strong>UseCosmosSql()<\/strong> method has been renamed to\u00a0UseCosmos()<\/li>\n<li>We now store owned entity references and collections in the same document as the owner<\/li>\n<li>Queries can now be executed asynchronously<\/li>\n<li><strong>SaveChanges()<\/strong>, <strong>EnsureCreated()<\/strong>, and <strong>EnsureDeleted()<\/strong> can now be executed synchronously<\/li>\n<li>You no longer need to manually generate unique key values for entities<\/li>\n<li>We preserve values in non-mapped properties when we update documents<\/li>\n<li>We added a <strong>ToContainer()<\/strong> API to map entity types to a Cosmos DB container (or collection) explicitly<\/li>\n<li>We now use the name of the derived <strong>DbContext<\/strong> type, rather &#8216;Unicorn&#8217; for the container or collection name we use by convention<\/li>\n<li>We enabled various existing features to work with Cosmos DB, including retrying execution strategies, and data seeding<\/li>\n<\/ul>\n<p>We still have some pending work and several limitations to remove in the provider. Most of them as tracked as uncompleted tasks <a href=\"https:\/\/github.com\/aspnet\/EntityFrameworkCore\/issues\/12086\">on our task list<\/a>. In addition to those:<\/p>\n<ul>\n<li>Currently, synchronous methods are much slower than the corresponding asynchronous methods<\/li>\n<li>The value of the &#8216;id&#8217; property has to be specified for seeding<\/li>\n<li>There is currently no enforcement of uniqueness of primary keys values on entities saved by multiple instances of the <strong>DbContext<\/strong><\/li>\n<\/ul>\n<p>In order to use the provider, install the 2.2 preview 3 version of the <strong>Microsoft.EntityFrameworkCore.Cosmos<\/strong>\u00a0package.<\/p>\n<p>The following example configures the <strong>DbContext<\/strong> to connect to the <a href=\"https:\/\/docs.microsoft.com\/en-us\/azure\/cosmos-db\/local-emulator\">Cosmos DB local emulator<\/a> to store a simple blogging model:<\/p>\n<pre><code class=\"csharp language-csharp\">\r\npublic class BloggingContext : DbContext\r\n{\r\n  public DbSet Blogs { get; set; }\r\n  public DbSet Posts { get; set; }\r\n\r\n  protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)\r\n  {\r\n    optionsBuilder.UseCosmos(\r\n      \"https:\/\/localhost:8081\",\r\n      \"C2y6yDjf5\/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw\/Jw==\",\r\n      \"MyDocuments\");\r\n  }\r\n}\r\n\r\npublic class Blog\r\n{\r\n  public int BlogId { get; set; }\r\n  public string Name { get; set; }\r\n  public string Url { get; set; }\r\n  public List Posts { get; set; }\r\n}\r\n\r\npublic class Post\r\n{\r\n  public int PostId { get; set; }\r\n  public string Title { get; set; }\r\n  public string Content { get; set; }\r\n  public List Tags { get; set; }\r\n}\r\n\r\n[Owned]\r\npublic class Tag\r\n{\r\n    [Key]\r\n    public string Name { get; set; }\r\n}\r\n<\/code><\/pre>\n<p>If you want, you can create the database programmatically, using EF Core APIs:<\/p>\n<pre><code class=\"csharp language-csharp\">\r\nusing (var context = new BloggingContext())\r\n{\r\n  context.Database.EnsureCreated();\r\n}\r\n<\/code><\/pre>\n<p>Once you have connected to an existing database and you have defined your entities, you can start storing data in the database, for example:<\/p>\n<pre><code class=\"csharp language-csharp\">\r\nusing (var context = new BloggingContext())\r\n{\r\n  context.Blogs.Add(\r\n    new Blog\r\n    {\r\n        BlogId = 1,\r\n        Name = \".NET Blog\",\r\n        Url = \"https:\/\/blogs.msdn.microsoft.com\/dotnet\/\",\r\n        Posts = new List\r\n        {\r\n            new Post\r\n            {\r\n                PostId = 2,\r\n                Title = \"Welcome to this blog!\",\r\n                Tags = new List\r\n                {\r\n                    new Tag\r\n                    {\r\n                        Name = \"Entity Framework Core\"\r\n                    },\r\n                    new Tag\r\n                    {\r\n                        Name = \".NET Core\"\r\n                    }\r\n                }\r\n            },\r\n        }\r\n      }\r\n    });\r\n  context.SaveChanges();\r\n}\r\n<\/code><\/pre>\n<p>And you can write queries using LINQ:<\/p>\n<pre><code class=\"csharp language-csharp\">\r\nvar dotNetBlog = context.Blogs.Single(b =&gt; b.Name == \".NET Blog\");\r\n<\/code><\/pre>\n<h3 id=\"query-tags\">Query tags<\/h3>\n<ul>\n<li>We fixed several issues with multiple calls of the API and with usage with multi-line strings<\/li>\n<li>The API was renamed to <strong>TagWith()<\/strong><\/li>\n<\/ul>\n<p>This an updated usage example:<\/p>\n<pre><code class=\"csharp language-csharp\">\r\n  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();\r\n<\/code><\/pre>\n<p>This will generate the following SQL output:<\/p>\n<pre><code class=\"sql language-sql\">\r\n-- 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\r\n<\/code><\/pre>\n<h3 id=\"collections-of-owned-entities\">Collections of owned entities<\/h3>\n<ul>\n<li>The main update since preview 2 is that the Cosmos DB provider now stores owned collections as part of the same document as the owner.<\/li>\n<\/ul>\n<p>Here is a simple usage scenario:<\/p>\n<pre><code class=\"csharp language-csharp\">\r\nmodelBuilder.Entity().OwnsMany(c =&gt; c.Addresses);\r\n<\/code><\/pre>\n<h2 id=\"thank-you\">Thank you<\/h2>\n<p>The EF team would like to thank everyone for all the feedback and contributions. Once more, please try this preview and report any feedback on <a href=\"https:\/\/github.com\/aspnet\/EntityFrameworkCore\/issues\/new\">our issue tracker<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Entity Framework Core 2.2 Preview 3 available with a new preview of our data provider for Cosmos DB and updated spatial extensions for various providers. Preview 3 is going to be the last milestone before EF Core 2.2 RTM, so now is your last chance to try the bits and give us feedback if you want to have an impact on the quality and the shape of the APIs in this release.<\/p>\n","protected":false},"author":912,"featured_media":21744,"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-19655","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>Entity Framework Core 2.2 Preview 3 available with a new preview of our data provider for Cosmos DB and updated spatial extensions for various providers. Preview 3 is going to be the last milestone before EF Core 2.2 RTM, so now is your last chance to try the bits and give us feedback if you want to have an impact on the quality and the shape of the APIs in this release.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/posts\/19655","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=19655"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/posts\/19655\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/media\/21744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/media?parent=19655"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/categories?post=19655"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/tags?post=19655"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}