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

在開始移轉程序之前務必驗證 EF 核心符合您的應用程式的資料存取需求。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 核心包含您需要在您的應用程式中使用的所有功能。Make sure that EF Core has all the features you need to use in your application. 請參閱功能比較的 EF 核心中設定的功能如何與 EF6 比較的詳細比較。See Feature Comparison for a detailed comparison of how the feature set in EF Core compares to EF6. 如果遺漏任何必要的功能,請確定您可以先移植到 EF 核心補償缺少這些功能。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 核心之間的行為中的某些變更非完整清單。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 核心執行類似的遞迴搜尋,但有一些稍有不同的規則。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 預設值 (也就是0intnullstring等。)。The primary key value is considered "not set" if it is assigned the CLR default value for the property type (i.e. 0 for int, null for string, etc.).

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

    • 如果主索引鍵不是產生的資料庫,實體是放在根狀態相同。If the primary key is not database generated, the entity is put in the same state as the root.

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

EF6 有大量的識別常數,它會執行選取的資料庫連接和初始化資料庫。這些規則包括: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 核心不會執行任何此識別常數。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 核心使用的名稱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.