Skip to main content

Command Palette

Search for a command to run...

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

Updated
1 min read
在分開的 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/")

More from this blog

Career-Ops 安裝與設定 — 用 AI 來幫你篩職缺、客製履歷

最近在看新的工作機會,光是瀏覽職缺、比對 JD、調整履歷就花了不少時間。 後來找到 Career-Ops 這個開源專案 作者 santifer 用這套系統篩了 740 多個職缺、產出 100 多份客製化履歷,最後拿到 Head of Applied AI 的 offer。 看起來蠻有意思的,所以我就在 Windows 上實際裝了一輪,這篇就來把整個安裝和設定的過程記錄下來。 我這邊搭配的 AI 後

Apr 12, 20265 min read
Career-Ops 安裝與設定 — 用 AI 來幫你篩職缺、客製履歷

讓 LLM 自動化你的 N8N 工作流程:n8n-mcp-server 安裝指南

前言 網路上一大堆都是在介紹如何在 N8N 裡面用 MCP 來呼叫其他工具,但是更多時候我希望 LLM 能直接幫我寫好或是修改 N8N 的工作流程 所以這篇就來介紹如何設定與使用 n8n-mcp-server 來讓 LLM 幫忙操控 N8N 在 GitHub 上有不少的 n8n-mcp-server,經過一下下的簡單搜索,我決定使用這個專案: n8n-mcp-server 原因在於說這個專案的實作提供了基本上所有需要編輯工作流程的功能,以下就來記錄安裝過程 MCP Server 設定 首先當然...

May 28, 20253 min read

[IT 鐵人賽] ASP.NET Core 與 Log 紀錄和追蹤的愛恨交織 - Day 05 - Elmah - 02

.NET Core Logging- Elmah 02 上一篇我們提到了基本的使用方式,接下來的這一篇我們就來講講要怎麼把 Log 放到不同的儲存體上,以及如何過濾 Log Log 儲存方式 我們先來看這些 Log 倒底存在哪裡,目前共有三種儲存方式,如下: MemoryErrorLog — store errors in memory 預設為使用此方式,簡單來說就是將錯誤 Log 都存在記憶體裡,所以只要應用程式一重新啟動,Log 就沒了;但是也是最方便的方式,只是要注意如果 Log 太...

Oct 20, 20182 min read
[IT 鐵人賽] ASP.NET Core 與 Log 紀錄和追蹤的愛恨交織 - Day 05 - Elmah - 02

[IT 鐵人賽] ASP.NET Core 與 Log 紀錄和追蹤的愛恨交織 - Day 04 - Elmah - 01

.NET Core Logging- Elmah 01 Elmah 是我最一開始使用的 Log 工具,基本上它的功能就是將應用程式所有發生的錯誤記錄下來,不需要改變程式架構,而且又有介面可以觀看,十分的方便;它從 WebForm 時代就有了,到了 MVC 也是運作的很好,那這邊就來介紹一下 Elmah 要如何在 ASP.NET Core 中使用 (.NET Core 也可使用)。 不過很可惜的,目前並沒有釋出官方的 Elmah for .NET Core,不過有一個新專案: ElmahCore...

Oct 19, 20181 min read
[IT 鐵人賽] ASP.NET Core 與 Log 紀錄和追蹤的愛恨交織 - Day 04 - Elmah - 01

[IT 鐵人賽] Azure Data Service - Day 04 - Cognitive Service - 辨識 - Video Indexer

Azure Data Service - Day 04 - Cognitive Service - Vision - Video Indexer 前面幾篇都是介紹靜態影像的辨識與分析,那這篇就來到針對影片來做處理。 Cognitive Service 針對影片辨識的部分提供了:Video Indexer 它整合了許多功能,在這邊列出一些比較重要的功能: 語系偵測 可以自動偵測出這個影片是屬於哪個語系,目前支援: English, Spanish, French, German, Italia...

Oct 19, 20181 min read
[IT 鐵人賽] Azure Data Service - Day 04 - Cognitive Service - 辨識 - Video Indexer

BrainBurst, HeadFirst

45 posts

學習學到老,撞牆撞到腦. A lifelong quest—with forehead dents.