之前從 EF6 移植到 EF Core: 驗證您的應用程式需求Before porting from EF6 to EF Core: Validate your Application's Requirements

在開始移轉程序之前務必驗證 EF Core 符合您的應用程式的資料存取需求。Before you start the porting process it is important to validate that EF Core meets the data access requirements for your application.

遺漏的功能Missing features

請確定 EF Core 包含您需要在您的應用程式中使用的所有功能。Make sure that EF Core has all the features you need to use in your application. 請參閱功能比較的 EF Core 中設定的功能如何與 EF6 比較的詳細比較。See Feature Comparison for a detailed comparison of how the feature set in EF Core compares to EF6. 如果遺漏任何必要的功能,請確定您可以先移植到 EF Core 補償缺少這些功能。If any required features are missing, ensure that you can compensate for the lack of these features before porting to EF Core.

行為變更Behavior changes

這是 EF6 和 EF Core 之間的行為中的某些變更非完整清單。This is a non-exhaustive list of some changes in behavior between EF6 and EF Core. 請務必牢記您的連接埠為您的應用程式因為它們可能會變更您的應用程式的行為,但是不會顯示以編譯錯誤互換之後到 EF Core 的方式。It is important to keep these in mind as your port your application as they may change the way your application behaves, but will not show up as compilation errors after swapping to EF Core.

DbSet.Add/Attach 和圖形的行為DbSet.Add/Attach and graph behavior

於 EF6,呼叫DbSet.Add()中遞迴搜尋來參考它的導覽屬性中的所有實體的實體結果。In EF6, calling DbSet.Add() on an entity results in a recursive search for all entities referenced in its navigation properties. 任何實體,找不到,並已不會追蹤內容,也會標示為已新增。Any entities that are found, and are not already tracked by the context, are also be marked as added. DbSet.Attach() 行為相同,但所有的實體會標示為不變。DbSet.Attach() behaves the same, except all entities are marked as unchanged.

EF Core 執行類似的遞迴搜尋,但有一些稍有不同的規則。EF Core performs a similar recursive search, but with some slightly different rules.

  • 根實體一定會處於要求的狀態 (加入DbSet.Add而不變的DbSet.Attach)。The root entity is always in the requested state (added for DbSet.Add and unchanged for DbSet.Attach).

  • 導覽屬性的遞迴搜尋期間找到的實體:For entities that are found during the recursive search of navigation properties:

    • 如果實體的主索引鍵是產生的存放區If the primary key of the entity is store generated

      • 如果未設定的主索引鍵的值,狀態會設定為已新增。If the primary key is not set to a value, the state is set to added. 主索引鍵值會被視為 「 未設定 」 指派的屬性類型的 CLR 預設值 (例如0針對intnullstring等。)。The primary key value is considered "not set" if it is assigned the CLR default value for the property type (for example, 0 for int, null for string, etc.).

      • 如果主索引鍵設定為值,狀態會設為不變。If the primary key is set to a value, the state is set to unchanged.

    • 主索引鍵不是產生的資料庫,實體會放入相同的狀態,以 root 身分執行。If the primary key is not database generated, the entity is put in the same state as the root.

程式碼的第一個資料庫初始化Code First database initialization

EF6 中有大量的 magic,它會執行選取的資料庫連線,以及將資料庫初始化。這些規則包括:EF6 has a significant amount of magic it performs around selecting the database connection and initializing the database. Some of these rules include:

  • 如果不執行任何設定,則 EF6 會選取 SQL Express 或 LocalDb 資料庫。If no configuration is performed, EF6 will select a database on SQL Express or LocalDb.

  • 如果應用程式中的連接字串具有相同名稱做為內容是App/Web.config檔案,將會使用此連接。If a connection string with the same name as the context is in the applications App/Web.config file, this connection will be used.

  • 如果資料庫不存在,它會建立它。If the database does not exist, it is created.

  • 如果沒有任何模型中的資料表存在於資料庫中,目前模型的結構描述會加入至資料庫。If none of the tables from the model exist in the database, the schema for the current model is added to the database. 如果已啟用移轉,它們用來建立資料庫。If migrations are enabled, then they are used to create the database.

  • 如果資料庫存在,而且 EF6 先前已建立結構描述,與目前模型的相容性檢查結構描述。If the database exists and EF6 had previously created the schema, then the schema is checked for compatibility with the current model. 如果模型已變更,因為已建立結構描述,則會擲回例外狀況。An exception is thrown if the model has changed since the schema was created.

EF Core 不會執行任何此識別常數。EF Core does not perform any of this magic.

  • 程式碼中,必須明確設定資料庫連接。The database connection must be explicitly configured in code.

  • 不會執行初始化。No initialization is performed. 您必須使用DbContext.Database.Migrate()以套用移轉 (或DbContext.Database.EnsureCreated()EnsureDeleted()建立/刪除資料庫而不使用移轉)。You must use DbContext.Database.Migrate() to apply migrations (or DbContext.Database.EnsureCreated() and EnsureDeleted() to create/delete the database without using migrations).

程式碼的第一個資料表命名慣例Code First table naming convention

EF6 執行複數表示服務,以計算預設的資料表名稱,此實體會對應至實體類別名稱。EF6 runs the entity class name through a pluralization service to calculate the default table name that the entity is mapped to.

EF Core 使用的名稱DbSet實體中公開衍生內容的屬性。EF Core uses the name of the DbSet property that the entity is exposed in on the derived context. 如果實體沒有DbSet用屬性,則類別名稱。If the entity does not have a DbSet property, then the class name is used.