EF Core – 7.0 New Features

前言

这篇不会细谈功能, 只是一个总链接.

 

参考

Docs – What's New in EF Core 7.0

 

Breaking Change

参考: Docs – Breaking changes in EF Core 7.0 (EF7)

Encrypt defaults to true for SQL Server connections

我 follow EF Core – 搭建单侧环境 做了一遍, 在运行 dotnet ef database update 后就出现了 error

A connection was successfully established with the server, but then an error occurred during the login process. (provider: SSL Provider, error: 0 - The certificate chain was issued by an authority that is not trusted.)

其原因是 EF7 依赖了 Microsoft.Data.SqlClient 5.0.0

Github Issue – EFCore.SqlServer 6.0.1 Untrusted certificate authority error

 

而 Microsoft.Data.SqlClient 4.0.0 的时候有一个 breaking change 

解决方法有好几个

参考: Stack Overflow – "The certificate chain was issued by an authority that is not trusted" when connecting DB in VM Role from Azure website

最简单的是在 ConnectionStrings 加上 TrustServerCertificate=True, 变成这样

Server=192.168.1.152;Database=EFCore7;Trusted_Connection=True;MultipleActiveResultSets=true;TrustServerCertificate=True

这个 for development 情况下是可以的, 在 production 的时候就不太理想了. 但我目前还没有 EF Core 7.0 production 的项目, 之后才研究 TODO...

SQL Server tables with triggers now require special EF Core configuration

EF7 对 multiple insert 在 SaveChanges 时做了一些优化 (快 4 倍), 但是在 SQL Server + Trigger (after insert) 的情况下, 这个优化跑不了.

所以当 Table 有使用 Trigger 就必须通知 EF Core 这样它就会 fallback 用回以前的 query.

modelBuilder
    .Entity<BlogWithTrigger>()
    .ToTable(tb => tb.HasTrigger("TRG_InsertUpdateBlog"));

Trigger 名字不重要, 因为只是要表达 "有" 而已. 如果想直接把所有 tables 都设置成 trigger 可以用这个 Model Convention.

 

JSON Columns

看这篇 EF Core – JSON Column

 

ExecuteUpdate and ExecuteDelete (Bulk updates)

看这篇 EF Core – ExecuteUpdate and ExecuteDelete (Bulk updates 批量更新和删除)

 

Index sort order

非常基础的一个功能, 到 7.0 才有...真的是...

如果只是一个 column, index order 对性能没有什么影响. 但如果是 multiple column 的 index, order 是可以提升性能的哦.

set all index to desc

modelBuilder.Entity<Blog>()
        .HasIndex(b => new { b.Url, b.Rating })
        .IsDescending();

set one by one, Url 是 asc, Rating 是 desc

modelBuilder.Entity<Blog>()
        .HasIndex(b => new { b.Url, b.Rating })
        .IsDescending(false, true);

 

posted @ 2022-09-26 12:46  兴杰  阅读(530)  评论(0编辑  收藏  举报