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 supported. 不過,其他作業(例如比較和順序)將需要用戶端上的評估。Other operations, however, like comparison and ordering will require evaluation on the client.

  • DateTimeOffsetDateTimeOffset
  • DecimalDecimal
  • TimeSpanTimeSpan
  • UInt64UInt64

我們建議使用 DateTime 值,而不是 DateTimeOffsetInstead 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.

@No__t-0 類型提供高的精確度。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 資料庫,將會擲回 NotSupportedExceptionIf 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
UpdateUpdate 2.02.0
DELETEDelete 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.