{"id":13346,"date":"2017-06-28T14:37:05","date_gmt":"2017-06-28T21:37:05","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/dotnet\/?p=13346"},"modified":"2021-09-30T10:08:22","modified_gmt":"2021-09-30T17:08:22","slug":"announcing-ef-core-2-0-preview-2","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/dotnet\/announcing-ef-core-2-0-preview-2\/","title":{"rendered":"Announcing EF Core 2.0 Preview 2"},"content":{"rendered":"<p>Today we are making Entity Framework Core 2.0 Preview 2 available.<\/p>\n<p>Entity Framework Core (EF Core) is a lightweight, extensible, and cross-platform version of Entity Framework. EF Core follows the same release cycle as .NET Core but can be used in multiple .NET platforms, including .NET Core 2.0 and .NET Framework 4.6.1 or newer.<\/p>\n<h2 id=\"installing-or-upgrading-to-20-preview-2\">Installing or upgrading to 2.0 Preview 2<\/h2>\n<h3 id=\"net-standard-20-requisite\">.NET Standard 2.0 requisite<\/h3>\n<p>From Preview 2 and beyond, EF Core 2.0 targets .NET Standard 2.0 and therefore requires .NET platforms that support it:<\/p>\n<ul>\n<li>If the application targets .NET Core, make sure that you install <a href=\"https:\/\/www.microsoft.com\/net\/core\/preview\">.NET Core SDK 2.0.0 Preview 2<\/a><\/li>\n<li>The minimum version of .NET Framework supported is 4.6.1<\/li>\n<li>For UWP applications this means that they cannot use EF Core 2.0 until a new release of UWP with support for .NET Standard 2.0 is released later this year. Existing UWP applications should continue to target EF Core 1.<\/li>\n<li>Mono and Xamarin versions with official support for .NET Standard 2.0 will also be available at a later date. In the meanwhile we encourage <a href=\"https:\/\/github.com\/aspnet\/entityframework\/issues\/new\">creating new issues<\/a> for anything that doesn&#8217;t work as expected<\/li>\n<\/ul>\n<p>See <a href=\"https:\/\/github.com\/aspnet\/Announcements\/issues\/246\">our announcement<\/a> for more details about the decision to target .NET Standard 2.0.<\/p>\n<h3 id=\"installing-new-runtime-packages\">Installing new runtime packages<\/h3>\n<p>In general, it should be possible to use Preview 2 by installing a 2.0 Preview 2-compatible version of an EF Core data provider.<\/p>\n<p>E.g. to install or upgrade the SQL Server provider from the command line in a .NET Core application for cross-platform development:<\/p>\n<pre><code class=\"console language-console\">$ dotnet add package Microsoft.EntityFrameworkCore.SqlServer -v 2.0.0-preview2-final<\/code><\/pre>\n<p>For any type of application using Visual Studio&#8217;s Package Manager Console:<\/p>\n<pre><code class=\"console language-console\">PM&gt; install-package Microsoft.EntityFrameworkCore.SqlServer -Version 2.0.0-preview2-final<\/code><\/pre>\n<p>Or to upgrade:<\/p>\n<pre><code class=\"console language-console\">PM&gt; update-package Microsoft.EntityFrameworkCore.SqlServer -Version 2.0.0-preview2-final<\/code><\/pre>\n<p>If you are using a third party database provider, then check to see if they have released an update that is compatible with 2.0.0-preview2-final. If they have, then just upgrade to the new version. If not, then you will not be able to upgrade since version 2.0 Preview 2 contains several breaking changes and existing providers are not expected to work with it.<\/p>\n<p>Applications targeting ASP.NET Core 2.0 Preview 2 can use EF Core 2.0 Preview 2 without additional dependencies besides third party data providers.<\/p>\n<p>Existing ASP.NET Core applications need to be upgrade to ASP.NET Core 2.0 Preview 2.<\/p>\n<p>Any references to older EF Core packages may need to be removed manually.<\/p>\n<h3 id=\"installing-new-tooling-packages\">Installing new tooling packages<\/h3>\n<p>If application targets .NET Core, we recommend following the instructions to install any pre-requisite updates to the development tools for the platform of your choice in the <a href=\"https:\/\/blogs.msdn.microsoft.com\/dotnet\/2017\/06\/28\/announcing-net-core-2-0-preview-2\/\">.NET Core 2.0 Preview 2 announcement post<\/a>.<\/p>\n<p>If your project references any of the tooling and design packages, then be sure to upgrade those too, e.g. in order to use the <code>dotnet ef<\/code> command line tools, your application&#8217;s CSPROJ file should contain the following:<\/p>\n<pre><code class=\"xml language-xml\">&lt;ItemGroup&gt;\n  &lt;DotNetCliToolReference\n      Include=\"Microsoft.EntityFrameworkCore.Tools.DotNet\"\n      Version=\"2.0.0-preview2-final\" \/&gt;\n&lt;\/ItemGroup&gt;<\/code><\/pre>\n<p>The Package Manager Console EF Core commands can be upgraded by issuing the following command:<\/p>\n<pre><code class=\"console language-console\">PM&gt; update-package Microsoft.EntityFrameworkCore.Tools -Version 2.0.0-preview2-final<\/code><\/pre>\n<h2 id=\"what-is-new-in-20-preview-2\">What is new in 2.0 Preview 2<\/h2>\n<p>Besides the improvements already contained in 2.0 Preview 1 and described in <a href=\"https:\/\/blogs.msdn.microsoft.com\/dotnet\/2017\/05\/12\/announcing-ef-core-2-0-preview-1\/\">our previous announcement<\/a>, here are details of some of the salient new features in Preview 2:<\/p>\n<h3 id=\"string-interpolation-in-fromsql-and-executesqlcommand\">String interpolation in FromSql and ExecuteSqlCommand<\/h3>\n<p>Preview 2 adds a feature that leverages C# 6.0 interpolated strings to build SQL at runtime in a way that helps avoid common mistakes that can lead to SQL injection. Here is an example:<\/p>\n<pre><code class=\"csharp language-csharp\">var city = \"London\";\nvar contactTitle = \"Sales Representative\";\n\nusing (var context = CreateContext())\n{\n    context.Customers\n       .FromSql($@\"\n           SELECT *\n           FROM Customers\n           WHERE City = {city}\n               AND ContactTitle = {contactTitle}\")\n       .ToArray();\n}<\/code><\/pre>\n<p>The query contains two variables embedded in the SQL format string. EF Core will produce the following SQL:<\/p>\n<pre><code class=\"sql language-sql\">@p0='London' (Size = 4000)\n@p1='Sales Representative' (Size = 4000)\n\nSELECT *\nFROM Customers\nWHERE City = @p0\n    AND ContactTitle = @p1<\/code><\/pre>\n<h3 id=\"automatic-table-splitting-for-owned-entity-types\">Automatic table splitting for owned entity types<\/h3>\n<p>In Preview 1 we already announced owned entity types as a way to create types that share the identity of their owner entity. In Preview 2 we have completed and refined the feature so that by convention owned types will share the table with their owner. E.g. for the following model only one table is created:<\/p>\n<pre><code class=\"csharp language-csharp\">modelBuilder.Entity&lt;Order&gt;().OwnsOne(\n    p =&gt; p.OrderDetails,\n    cb =&gt;\n    {\n        cb.OwnsOne(c =&gt; c.BillingAddress);\n        cb.OwnsOne(c =&gt; c.ShippingAddress);\n    });\n\npublic class Order\n{\n    public int Id { get; set; }\n    public OrderDetails OrderDetails { get; set; }\n}\n\npublic class OrderDetails\n{\n    public StreetAddress BillingAddress { get; set; }\n    public StreetAddress ShippingAddress { get; set; }\n}\n\npublic class StreetAddress\n{\n    public string Street { get; set; }\n    public string City { get; set; }\n}<\/code><\/pre>\n<h3 id=\"database-scalar-function-mapping\">Database scalar function mapping<\/h3>\n<p>Preview 2 includes an important contribution from <a href=\"https:\/\/github.com\/pmiddleton\">Paul Middleton<\/a> which enables mapping database scalar functions to method stubs so that they can be used in LINQ queries and translated to SQL.<\/p>\n<p>The API is still in flux after Preview 2 but here is a brief description of how the feature can be used:<\/p>\n<p>Declare a static method on your <code>DbContext<\/code> and annotate it with <code>DbFunctionAttribute<\/code>:<\/p>\n<pre><code class=\"csharp language-csharp\">public class BloggingContext : DbContext\n{\n    [DbFunction]\n    public static int PostReadCount(int blogId)\n    {\n        throw new Exception();\n    }\n}<\/code><\/pre>\n<p>Methods like this are automatically registered. Once a method has been registered you can use it anywhere in your queries:<\/p>\n<pre><code class=\"csharp language-csharp\">var query =\n    from p in context.Posts\n    where BloggingContext.PostReadCount(p.Id) &gt; 5\n    select p;<\/code><\/pre>\n<p>A few things to note:<\/p>\n<ul>\n<li>By convention the name of the method is used as the name of a function (in this case a user defined function) when generating the SQL, but you can override the name and schema during method registration<\/li>\n<li>Currently only scalar functions are supported<\/li>\n<li>You must create the mapped function in the database, e.g. EF Core migrations will not take care of creating it<\/li>\n<\/ul>\n<h2 id=\"breaking-changes-in-20\">Breaking changes in 2.0<\/h2>\n<p>We have taken the opportunity to significantly refine our existing APIs and behaviors in 2.0. There are a few improvements that can require modifying existing application code, although we believe that for the majority of applications the impact will be low, in most cases requiring just recompilation and minimal guided changes to replace obsolete APIs.<\/p>\n<p>While we are putting together a more complete announcement of the breaking changes, we wanted to highlight some of the important types of changes here:<\/p>\n<h3 id=\"provider-and-extensions-compatibility\">Provider and extensions compatibility<\/h3>\n<p><strong>EF Core 2.0 will not be compatible with existing providers.<\/strong> Unlike the standard application development API, the impact on providers surface will be significant and we are happy to work with provider writers to help them through the transition. The best way to contact us if you are working on moving an existing provider and need help, is to <a href=\"https:\/\/github.com\/aspnet\/entityframework\/issues\/new\">create a new issue<\/a> on our GitHub repository.<\/p>\n<p>There are also changes in advanced API areas such as metadata and service interfaces, which might affect libraries that extend EF Core. Once again, we are happy to work through the transition with extension authors.<\/p>\n<h3 id=\"binary-compatibility\">Binary compatibility<\/h3>\n<p><strong>EF Core 2.0 won&#8217;t be binary compatible with EF Core 1<\/strong>, which means that all code that depends on EF Core needs to at least be recompiled to work with 2.0.<\/p>\n<p>In fact, many changes we made break existing compiled code but don&#8217;t require changes to source code, just recompilation. A canonical example of this is when we updated of our <code>Include<\/code> APIs to support collection navigation properties that are of type <code>IEnumerable&lt;T&gt;<\/code> rather than <code>ICollection&lt;T&gt;<\/code> in 1.1. We realized such binary breaking changes was not acceptable in a point release so we brought back binary compatible methods in 1.1.1. But now that 2.0 has new target requirements it requires libraries and application code to be recompiled anyway, and the old version of the API based on <code>ICollection&lt;T&gt;<\/code> has been removed.<\/p>\n<p>Other examples are types we moved to more commonly used namespaces, e.g. the <code>DeleteBehavior<\/code> enum was moved to the main <code>Microsoft.EntityFrameworkCore<\/code> namespace.<\/p>\n<h3 id=\"obsolete-apis\">Obsolete APIs<\/h3>\n<p>Examples of these are the <code>UseInMemoryDatabase<\/code> overload that does not take a name for the database, and the replacement of <code>IDbContextFactory<\/code> with <code>IDesignTimeDbContextFactory<\/code> in a different namespace.<\/p>\n<p>While APIs marked as obsolete can be removed in future releases, and their usage will cause compile time warnings, they will continue to work for this release.<\/p>\n<h3 id=\"obsolete-packages\">Obsolete packages<\/h3>\n<p>As part of the refactoring of our provider model, in Preview 2 we have merged the functionality previously contained in provider&#8217;s design-time packages into the main (and now only) provider package. E.g. there won&#8217;t be any more updates of <code>Microsoft.EntityFrameworkCore.SqlServer.Design<\/code> and any reference to it should be removed.<\/p>\n<h3 id=\"behavioral-changes\">Behavioral changes<\/h3>\n<p>An example of this is the adjustments to support the new patterns for design-time discovery of <code>DbContext<\/code> types for ASP.NET Core 2.0 applications based on <code>Program.BuildWebHost()<\/code> and the removal of the support for the 1.x pattern based only on <code>Startup.ConfigureServices()<\/code>.<\/p>\n<p>Another good example is that starting with Preview 2 we throw an exception any time we find an unnecessary call to <code>Include()<\/code> in a query.<\/p>\n<p>E.g. the following query now throws, since <code>Include()<\/code> is not necessary to make the <code>Where<\/code> clause work, and we cannot use it to load <code>Product<\/code> instances alongside the results, because the <code>Select<\/code> clause changed the results so that the query no longer returns instances of <code>Order<\/code>:<\/p>\n<pre><code class=\"csharp language-csharp\">var pids = context.Orders\n    .Include(o =&gt; o.Product)\n    .Where(o =&gt; o.Product.Name == \"Baked Beans\")\n    .Select(o =&gt;o.ProductId)\n    .ToList();<\/code><\/pre>\n<p>The exception can be avoided by either removing the unnecessary <code>Include()<\/code> call or by configuring this particular case to log a warning instead of causing an error in either <code>OnConfiguring<\/code> or the <code>AddDbContext<\/code> call:<\/p>\n<pre><code class=\"csharp language-csharp\">optionsBuilder.ConfigureWarnings(\n    w =&gt; w.Log(CoreEventId.IncludeIgnoredWarning));<\/code><\/pre>\n<h2 id=\"what-is-next-after-preview-2\">What is next after Preview 2<\/h2>\n<p>At this point in the release cycle, we are focusing most of our time an effort on fixing bugs and making only a few final API adjustments. We encourage you to continue providing feedback, in particular about your experience using the new features. Once more, the best way to give us feedback is to <a href=\"https:\/\/github.com\/aspnet\/entityframework\/issues\/new\">create a new issue<\/a> in out GitHub repository.<\/p>\n<h2 id=\"thank-you\">Thank you!<\/h2>\n<p>We want to take the opportunity to thank all the members of the .NET developer community who with their feedback are helping make EF Core 2.0 a better in release, in particular those who made code contributions in Preview 2: Paul Middleton (<a href=\"https:\/\/github.com\/pmiddleton\">@pmiddleton<\/a>) and Erik Ejlskov Jensen (<a href=\"https:\/\/github.com\/ErikEJ\">@ErikEJ<\/a>).<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Today we are making Entity Framework Core 2.0 Preview 2 available. Entity Framework Core (EF Core) is a lightweight, extensible, and cross-platform version of Entity Framework. EF Core follows the same release cycle as .NET Core but can be used in multiple .NET platforms, including .NET Core 2.0 and .NET Framework 4.6.1 or newer. Installing [&hellip;]<\/p>\n","protected":false},"author":912,"featured_media":58792,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[685],"tags":[9,32,70,71],"class_list":["post-13346","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-dotnet","tag-net-core","tag-asp-net-core","tag-entity-framework","tag-entity-framework-core"],"acf":[],"blog_post_summary":"<p>Today we are making Entity Framework Core 2.0 Preview 2 available. Entity Framework Core (EF Core) is a lightweight, extensible, and cross-platform version of Entity Framework. EF Core follows the same release cycle as .NET Core but can be used in multiple .NET platforms, including .NET Core 2.0 and .NET Framework 4.6.1 or newer. Installing [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/posts\/13346","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=13346"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/posts\/13346\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/media\/58792"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/media?parent=13346"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/categories?post=13346"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/tags?post=13346"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}