2.0.0-priview1-final, so this article is mainly about some instructions for this version.
mysql tutorial"
// 安装 PM> install-package Microsoft.EntityFrameworkCore.SqlServer -Pre -Version 2.0.0-preview1-final // 升级 PM> update-package Microsoft.EntityFrameworkCore.SqlServer -Pre -Version 2.0.0-preview1-final
// 直接修改 CSPROJ 文件 <ItemGroup> <DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.0-preview1-final" /> </ItemGroup> // 或者通过以下命令 PM> update-package Microsoft.EntityFrameworkCore.Tools -Pre -Version 2.0.0-preview1-final
var aCustomers = from c in context.Customers where EF.Functions.Like(c.Name, "a%"); select c;
modelBuilder.Entity<Customer>() .OwnsOne(c => c.WorkAddress);public 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; } }
Customer and
Address will be generated as one table.
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()). Of course you can use
IgnoreQueryFilters() to disable this global filter in the query.
on every request. In EF Core 2.0, a new way of injecting a custom DbContext is introduced, which explicitly uses an instance pool to inject it into the container.
services.AddDbContextPool<BloggingContext>( options => options.UseSqlServer(connectionString));
Using this method, when the Controller requests a DbContext instance, it will first check whether there is an available instance in the pool. Once the request is processed, any state attached to the instance will be reset. , and then the instance is returned to the pool.
This concept is somewhat similar to the database connection pool in ADO.NET, which has the advantage of saving the cost of initializing a DbContext instance. Many ASP.NET Core applications can use this approach to gain performance improvements.
Manually compiled queries
Although EF Core can automatically compile and cache queries based on query expressions, this mechanism can obtain a small query performance improvement by bypassing hash calculations or caching, allowing applications to use delegates that have already been called. Query passed by chain compilation.
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); }
Others
The addition of Lazy Loading (lazy loading) function is still under discussion, and this function may be added in EF Core 2.1.
The above is the detailed content of EF Core 2.0 new features. For more information, please follow other related articles on the PHP Chinese website!