EF Core 2.2 中的新增功能New features in EF Core 2.2

空间数据支持Spatial data support

空间数据可用于表示对象的物理位置和形状。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 现在支持使用 NetTopologySuite (NTS) 库中的类型,处理各种数据库中的空间数据。EF Core 2.2 now supports working with spatial data from various databases using types from the NetTopologySuite (NTS) library.

空间数据支持是作为一系列提供程序专用扩展包进行实现。Spatial data support is implemented as a series of provider-specific extension packages. 每个包都为 NTS 类型和方法以及数据库中相应的空间类型和函数提供映射。Each of these packages contributes mappings for NTS types and methods, and the corresponding spatial types and functions in the database. 此类提供程序扩展现在可用于 SQL ServerSQLitePostgreSQL(来自 Npgsql 项目)。Such provider extensions are now available for SQL Server, SQLite, and PostgreSQL (from the Npgsql project). 空间类型可以直接与 EF Core 内存中提供程序一起使用,无需使用额外扩展。Spatial types can be used directly with the EF Core in-memory provider without additional extensions.

安装提供程序扩展后,便能向实体添加受支持类型的属性。Once the provider extension is installed, you can add properties of supported types to your entities. 例如:For example:

using NetTopologySuite.Geometries;

namespace MyApp
  public class Friend
    public string Name { get; set; }
    public Point Location { get; set; }

然后,可以暂留包含空间数据的实体:You can then persist entities with spatial data:

using (var context = new MyDbContext())
        new Friend
            Name = "Bill",
            Location = new Point(-122.34877, 47.6233355) {SRID = 4326 }

还可以根据空间数据和操作执行数据库查询:And you can execute database queries based on spatial data and operations:

  var nearestFriends =
      (from f in context.Friends
      orderby f.Location.Distance(myLocation) descending
      select f).Take(5).ToList();

若要详细了解此功能,请参阅空间类型文档For more information on this feature, see the spatial types documentation.

从属实体集合Collections of owned entities

EF Core 2.0 增加了在一对一关联中对所有权进行建模的功能。EF Core 2.0 added the ability to model ownership in one-to-one associations. EF Core 2.2 将此功能扩展为,将所有权表达为一对多关联。EF Core 2.2 extends the ability to express ownership to one-to-many associations. 所有权有助于约束实体的使用方式。Ownership helps constrain how entities are used.

例如,从属实体:For example, owned entities:

  • 只能出现在其他实体类型的导航属性中。Can only ever appear on navigation properties of other entity types.
  • 自动加载,并且只能被 DbContext 和所有者跟踪。Are automatically loaded, and can only be tracked by a DbContext alongside their owner.

在关系数据库中,从属集合映射到所有者的各个表,就像是常规的一对多关联一样。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.

若要使用此功能,可以调用新增的 OwnsMany() API:You can use the feature by calling the new OwnsMany() API:

modelBuilder.Entity<Customer>().OwnsMany(c => c.Addresses);

有关详细信息,请参阅更新后的从属实体文档For more information, see the updated owned entities documentation.

查询标记Query tags

此功能简化了将代码中的 LINQ 查询与日志中捕获的已生成 SQL 查询相关联的过程。This feature simplifies the correlation of LINQ queries in code with generated SQL queries captured in logs.

若要利用查询标记,请使用新增的 TagWith() 方法对 LINQ 查询进行批注。To take advantage of query tags, you annotate a LINQ query using the new TagWith() method. 使用上一示例中的空间查询:Using the spatial query from a previous example:

  var nearestFriends =
      (from f in context.Friends.TagWith(@"This is my spatial query!")
      orderby f.Location.Distance(myLocation) descending
      select f).Take(5).ToList();

此 LINQ 查询将生成以下 SQL 输出:This LINQ query will produce the following SQL output:

-- This is my spatial query!

SELECT TOP(@__p_1) [f].[Name], [f].[Location]
FROM [Friends] AS [f]
ORDER BY [f].[Location].STDistance(@__myLocation_0) DESC

有关详细信息,请参阅查询标记文档For more information, see the query tags documentation.