介绍
接下来我将给大家重点介绍一下.Net 6 之后的一些新的变更,文章都是来自于外国大佬的文章,我这边进行一个翻译,并加上一些自己的理解和解释。
源作者链接:https://blog.okyrylchuk.dev/entity-framework-core-6-features-part-3
正文
SQLite 支持 DateOnly 和 TimeOnly
SQLite 提供程序在 EF Core 6.0 中支持新的DateOnly和TimeOnly类型。它将它们存储为TEXT。
using var context = new ExampleContext();
var query1 = context.People
.Where(p => p.Birthday < new DateOnly(2000, 1, 1))
.ToQueryString();
Console.WriteLine(query1);
// SELECT "p"."Id", "p"."Birthday", "p"."Name"
// FROM "People" AS "p"
// WHERE "p"."Birthday" < '2000-01-01'
var query2 = context.Notifications
.Where(n => n.AllowedFrom >= new TimeOnly(8, 0) && n.AllowedTo <= new TimeOnly(16, 0))
.ToQueryString();
Console.WriteLine(query2);
// SELECT "n"."Id", "n"."AllowedFrom", "n"."AllowedTo"
// FROM "Notifications" AS "n"
// WHERE("n"."AllowedFrom" >= '08:00:00') AND("n"."AllowedTo" <= '16:00:00')
class Person
{
public int Id { get; set; }
public string Name { get; set; }
public DateOnly Birthday { get; set; }
}
class Notification
{
public int Id { get; set; }
public TimeOnly AllowedFrom { get; set; }
public TimeOnly AllowedTo { get; set; }
}
class ExampleContext : DbContext
{
public DbSet<Person> People { get; set; }
public DbSet<Notification> Notifications { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder options)
=> options.UseSqlite(@"Data Source=Db\DateOnlyTimeOnly.db");
}
SQLite 连接是池化的
SQLite 数据库是一个文件。因此,大多数情况下创建连接速度很快。但是,打开与加密数据库的连接可能会非常慢。因此,SQLite 连接现在像 EF Core 6 中的其他数据库提供程序一样被池化。
class Person
{
public int Id { get; set; }
public string Name { get; set; }
}
class ExampleContext : DbContext
{
public DbSet<Person> People { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder options)
=> options.UseSqlite("Data Source=EncryptedDb.db;Mode=ReadWriteCreate;Password=password");
}
SQLite 中的命令超时
在 EF Core 6 for SQLite 中,已将命令超时添加到连接字符串。SQLite 将Default Timeout视为Command Timeout的同义词,如果愿意,可以使用它来代替。
class Person
{
public int Id { get; set; }
public string Name { get; set; }
}
class ExampleContext : DbContext
{
public DbSet<Person> People { get; set; }
// 60 seconds as the default timeout for commands created by connection
protected override void OnConfiguring(DbContextOptionsBuilder options)
=> options.UseSqlite("Data Source=Test.db;Command Timeout=60");
}
结语
联系作者:加群:867095512 @MrChuJiu