SQLite EF Core 数据库提供程序限制SQLite EF Core Database Provider Limitations

SQLite 提供程序有很多迁移限制。The SQLite provider has a number of migrations limitations. 其中的大多数限制是由基础 SQLite 数据库引擎中的限制引起的,并不特定于 EF。Most of these limitations are a result of limitations in the underlying SQLite database engine and are not specific to EF.

建模限制Modeling limitations

公共关系库 (实体框架关系数据库提供程序共享,) 定义 Api,用于建模大多数关系数据库引擎所共有的概念。The common relational library (shared by Entity Framework relational database providers) defines APIs for modelling concepts that are common to most relational database engines. SQLite 提供程序不支持其中几个概念。A couple of these concepts are not supported by the SQLite provider.

  • 架构Schemas
  • 序列Sequences

查询限制Query limitations

SQLite 本身并不支持以下数据类型。SQLite doesn't natively support the following data types. EF Core 可以读取和写入这些类型的值,并且还支持) 的相等性 (查询 where e.Property == valueEF Core can read and write values of these types, and querying for equality (where e.Property == value) is also supported. 但其他操作(如比较和排序)将需要对客户端进行评估。Other operations, however, like comparison and ordering will require evaluation on the client.

  • DateTimeOffsetDateTimeOffset
  • 小数Decimal
  • TimeSpanTimeSpan
  • UInt64UInt64

DateTimeOffset建议使用 DateTime 值,而不是。Instead of DateTimeOffset, we recommend using DateTime values. 处理多个时区时,建议在保存之前将值转换为 UTC,然后将其转换回适当的时区。When handling multiple time zones, we recommend converting the values to UTC before saving and then converting back to the appropriate time zone.

Decimal类型提供了较高的精度。The Decimal type provides a high level of precision. 但是,如果不需要该级别的精度,则建议改为使用 double。If you don't need that level of precision, however, we recommend using double instead. 您可以使用 值转换器 在类中继续使用 decimal。You can use a value converter to continue using decimal in your classes.

modelBuilder.Entity<MyEntity>()
    .Property(e => e.DecimalProperty)
    .HasConversion<double>();

迁移限制Migrations limitations

SQLite 数据库引擎不支持许多其他关系数据库所支持的架构操作。The SQLite database engine does not support a number of schema operations that are supported by the majority of other relational databases. 如果尝试将不受支持的操作之一应用于 SQLite 数据库,则 NotSupportedException 会引发。If you attempt to apply one of the unsupported operations to a SQLite database then a NotSupportedException will be thrown.

将尝试重新生成以执行特定操作。A rebuild will be attempted in order to perform certain operations. 重新生成仅可用于作为 EF Core 模型一部分的数据库项目。Rebuilds are only possible for database artifacts that are part of your EF Core model. 如果数据库项目不是模型的一部分,例如,如果它是在迁移内手动创建的,则 NotSupportedException 仍会引发。If a database artifact isn't part of the model--for example, if it was created manually inside a migration--then a NotSupportedException is still thrown.

操作Operation 支持?Supported? 需要版本Requires version
AddCheckConstraintAddCheckConstraint 重新生成 (✔) ✔ (rebuild) 5.05.0
AddColumnAddColumn 1.01.0
AddForeignKeyAddForeignKey 重新生成 (✔) ✔ (rebuild) 5.05.0
AddPrimaryKeyAddPrimaryKey 重新生成 (✔) ✔ (rebuild) 5.05.0
AddUniqueConstraintAddUniqueConstraint 重新生成 (✔) ✔ (rebuild) 5.05.0
AlterColumnAlterColumn 重新生成 (✔) ✔ (rebuild) 5.05.0
CreateIndexCreateIndex 1.01.0
CreateTableCreateTable 1.01.0
DropCheckConstraintDropCheckConstraint 重新生成 (✔) ✔ (rebuild) 5.05.0
DropColumnDropColumn 重新生成 (✔) ✔ (rebuild) 5.05.0
DropForeignKeyDropForeignKey 重新生成 (✔) ✔ (rebuild) 5.05.0
DropIndexDropIndex 1.01.0
DropPrimaryKeyDropPrimaryKey 重新生成 (✔) ✔ (rebuild) 5.05.0
DropTableDropTable 1.01.0
DropUniqueConstraintDropUniqueConstraint 重新生成 (✔) ✔ (rebuild) 5.05.0
RenameColumnRenameColumn 2.2.22.2.2
RenameIndexRenameIndex 重新生成 (✔) ✔ (rebuild) 2.12.1
RenameTableRenameTable 1.01.0
EnsureSchemaEnsureSchema ✔ (的) ✔ (no-op) 2.02.0
DropSchemaDropSchema ✔ (的) ✔ (no-op) 2.02.0
插入Insert 2.02.0
更新Update 2.02.0
删除Delete 2.02.0

迁移限制解决方法Migrations limitations workaround

通过在迁移中手动编写代码来执行重新生成,可以解决其中一些限制。You can workaround some of these limitations by manually writing code in your migrations to perform a rebuild. 表重建涉及创建新表,将数据复制到新表,删除旧表,重命名新表。Table rebuilds involve creating a new table, copying data to the new table, dropping the old table, renaming the new table. 你将需要使用 Sql(string) 方法来执行其中一些步骤。You will need to use the Sql(string) method to perform some of these steps.

有关更多详细信息,请参阅在 SQLite 文档中 进行其他类型的表架构更改See Making Other Kinds Of Table Schema Changes in the SQLite documentation for more details.