New features of EF Core 2.0

Source: Internet
Author: User

Objective

The latest version of EF Core is currently available 2.0.0-priview1-final , so this article focuses on some of the instructions for this release.

Note: If you want to use. NET Core 2.0 in Visual Studio, you need at least Visual Studio 2017 15.3 Preview version.

Install or upgrade to EF Core 2.0

You can install or upgrade your current. NET Core version with the following command.

Install pm> install-package microsoft.entityframeworkcore.sqlserver-pre-version 2.0.0-preview1-final//upgrade pm> Update-package microsoft.entityframeworkcore.sqlserver-pre-version 2.0.0-preview1-final

Tool Pack

Directly modify the CSPROJ file <ItemGroup>   <dotnetclitoolreference include= " Microsoft.EntityFrameworkCore.Tools.DotNet "        version=" 2.0.0-preview1-final "/></itemgroup>// or pm> update-package microsoft.entityframeworkcore.tools-pre-version 2.0.0-preview1-final with the following command

New EF Core 2.0 features

Improved LINQ translation

    • Avoid creating unnecessary sub-queries

    • Some commands will switch to the client for execution

    • Only a few requests will retrieve all columns of the table

    • There are no appropriate filtering conditions to convert a single LINQ query to N + 1 queries.

Ef. Functions.like ()

EF was added in EF Core 2.0. Functions Properties, EF Core Provider can use them to customize some of the methods that map to the latter operators of database functions, so that they are called in LINQ queries. Such as:


var acustomers = from    C in context. Customers    where EF. Functions.like (C.name, "a%");    Select C;

Detaching entities and tables

Separating entities and tables what does that mean? Previously, a database table was mapped to an entity object in EF, that is, the table and entity are one by one corresponding relationships. In version 2.0, it is allowed to map some associated entities into a table, and EF maintains these instances or referential relationships.


Modelbuilder.entity<customer> ()    . Ownsone (c = c.workaddress);p ublic class customer{public    int CustomerId {get; set;}    Public Address workaddress {get; set;}} public class address{Public    string Line {get; set;}    public string Postalorzipcode {get; set;}    public string Stateorprovince {get; set;}    public string Cityortown {get; internal set;}}

When a database table is generated, Customer it Address is created as a table.

Note: This feature is temporarily incomplete in Priview1.

Global Query filtering

The new version introduces a feature called "Vertical Filtering", which is a more common requirement.

When we define the EF core context model, we can add some filters when the model is created, such as always filter out some "tombstone" data when querying.

public class bloggingcontext:dbcontext{Public    dbset<blog> Blogs {get; set;}    Public dbset<post> Posts {get; set;}    public int TenantId {get; set;}    protected override void Onmodelcreating (ModelBuilder ModelBuilder)    {        modelbuilder.entity<post> ()            . Hasqueryfilter (p =!p.isdeleted &&                  P.tenantid = = this. TENANTID);}    }

Include()This filter is automatically applied when querying type data through a direct query or a navigation property (). Of course you can use it IgnoreQueryFilters() to disable this global filter in the query.

DbContext Connection Pool

Typically, using EF core in ASP. NET core involves a custom DbContext, which is then injected into the system container and then fetched from the container by the Controller's constructor to get the object instance. This also means that a new instance will be created in each request .

In EF Core 2.0, a new way of injecting custom dbcontext is introduced, which shows how an instance pool is used to inject into a container.

Services. adddbcontextpool<bloggingcontext> (    options = options. Usesqlserver (connectionString));

In this way, when the Controller requests the DbContext instance, it will first check if there are any available instances in the pool, and once the request processing is complete, any state attached to the instance will be reset and the instance will be returned to the pool again.

This concept is somewhat similar to the database connection pool in ADO, which has the advantage of saving the cost of initializing DbContext instances. Many ASP. NET Core applications can use this approach to achieve performance improvements.

Compiling queries manually

There are APIs in previous versions of EF and LINQ to SQL that can be manually or display compiled queries that allow applications to cache queries that have already been translated so that they can be compiled only once and executed multiple times.

Although EF Core can automatically compile and cache queries based on query expressions, this mechanism can get a small query performance boost by bypassing hash calculations or caches, allowing applications to use queries that have been compiled by invoking a delegate chain.


private static Func<customercontext, int, customer> _customerbyid =    EF. Compilequery ((customercontext db, int id) = =        db. Customers            . Include (c = c.address)            . Single (c = c.id = = Id));.. using (var db = new Customercontext ()) {   var customer = _customerbyid (db, 147);}

Other

EF Core 2.0 will also make some significant adjustments to some logging and the infrastructure for diagnostics, as well as integration with the Azure application Insights.

The increase in lazy Loading (lazy load) functionality is currently under discussion and may be added to EF Core 2.1.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.