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

(Entity Framework 的關聯式資料庫提供者所共用) 的通用關聯式程式庫會定義 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
  • 計算資料行Computed columns

查詢限制Query limitations

SQLite 原生不支援下列資料類型。SQLite doesn't natively support the following data types. EF Core 可以讀取和寫入這些類型,並查詢是否相等的值 (where e.Property == value) 也是支援。EF Core can read and write values of these types, and querying for equality (where e.Property == value) is also support. 其他作業,不過,例如比較和排序會需要用戶端上的評估。Other operations, however, like comparison and ordering will require evaluation on the client.

  • DateTimeOffsetDateTimeOffset
  • DecimalDecimal
  • TimeSpanTimeSpan
  • UInt64UInt64

而不是DateTimeOffset,我們建議使用日期時間值。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. 如果您不需要該層級的有效位數,不過,建議改為使用雙引號。If you don't need that level of precision, however, we recommend using double instead. 您可以使用值轉換器繼續使用在類別中的十進位。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.

運算Operation 支援?Supported? 需要版本Requires version
AddColumnAddColumn 1.01.0
AddForeignKeyAddForeignKey
AddPrimaryKeyAddPrimaryKey
AddUniqueConstraintAddUniqueConstraint
AlterColumnAlterColumn
CreateIndexCreateIndex 1.01.0
CreateTableCreateTable 1.01.0
DropColumnDropColumn
DropForeignKeyDropForeignKey
DropIndexDropIndex 1.01.0
DropPrimaryKeyDropPrimaryKey
DropTableDropTable 1.01.0
DropUniqueConstraintDropUniqueConstraint
RenameColumnRenameColumn 2.2.22.2.2
RenameIndexRenameIndex 2.12.1
RenameTableRenameTable 1.01.0
EnsureSchemaEnsureSchema ✔ (no-op)✔ (no-op) 2.02.0
DropSchemaDropSchema ✔ (no-op)✔ (no-op) 2.02.0
InsertInsert 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 table rebuild. 重建資料表需要重新命名現有的資料表、建立新的資料表、將資料複製到新的資料表,以及卸除舊的資料表。A table rebuild involves renaming the existing table, creating a new table, copying data to the new table, and dropping the old 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.

在未來,EF 可能支援這些作業的一些使用在幕後的資料表重建方法。In the future, EF may support some of these operations by using the table rebuild approach under the covers. 您可以追蹤這項功能,在我們的 GitHub 專案You can track this feature on our GitHub project.