在分開的 Class Library 專案中使用 Entity Framework Core 進行 Migrations

Entity Framework Core Migrations at separate class library project Implement IDesignTimeDbContextFactory

Entity Framework Core Database First 根據官方資料目前尚未正式支援 Many-to-Many (GitHub issue 1368),有 Workround 但是不漂亮,所以就乾脆放棄 Database First 方式,改回來用 Code First,那遇到的問題又重頭了,怎麼在分開的 Class Library 專案中使用 Entity Framework Core Code First,這邊就簡單紀錄一下
Code First 的方式當然是一模一樣,所以這邊就不多介紹了,可以參考,但是在 Add Migration 或是 Update Database 時就會出現以下錯誤訊息
Unable to create an object of type ‘MmsFactoryCoreContext’. Add an implementation of ‘IDesignTimeDbContextFactory’ to the project, or see https://go.microsoft.com/fwlink/?linkid=851728 for additional patterns supported at design time.

IDesignTimeDbContextFactory
解決方法
簡單來說,我們要假裝這支 Class Library 是 .NetCore App
首先我們先如同 Console App 一樣,新增 Program.cs 檔案於根目錄下
我們需要先指定資料庫連線字串,當然直接寫死在程式裡也可以,但是身為有素質的程式人,怎麼可以把東西寫死,還是於根目錄新增 appsettings.json,裏面包含了資料庫連線字串
然後然後我們就可以開始實作 IDesignTimeDbContextFactory 介面,用途就是用來建立 DbContext,這邊就取巧不再開新檔案,而是直接放在 Program.cs 裡
其中用到 Configration 相關組件,所以需要安裝以下 Nuget
- Microsoft.Extensions.Configuration
- Microsoft.Extensions.Configuration.FileExtensions
- Microsoft.Extensions.Configuration.Json
到此就完成了所有前置步驟,可以開始下指令了

Complete
參考
[CodingBlast Solution to the EntityFramework Core issue - Add an implementation of 'IDesignTimeDbContextFactory' to the project.codingblast.com](https://codingblast.com/entityframework-core-idesigntimedbcontextfactory/ "https://codingblast.com/entityframework-core-idesigntimedbcontextfactory/")
[Entity Framework Core Migrations for Class Library Projects Entity Framework Core 1.0.0 RTM still does not support migrations for class library projects. If you're like me and…benjii.me](http://benjii.me/2016/06/entity-framework-core-migrations-for-class-library-projects/ "http://benjii.me/2016/06/entity-framework-core-migrations-for-class-library-projects/")

![[IT 鐵人賽] ASP.NET Core 與 Log 紀錄和追蹤的愛恨交織 - Day 05 - Elmah - 02](/_next/image?url=https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1776070454149%2Fa0702417-5357-4217-a572-7ba53ea36735.png&w=3840&q=75)
![[IT 鐵人賽] Azure Data Service - Day 04 - Cognitive Service - 辨識 - Video Indexer](/_next/image?url=https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1776070473207%2F6423fd4a-4041-4bb2-b918-4cfe3f9aea5e.png&w=3840&q=75)