當(dāng)我們?cè)趧?chuàng)建 Service Fabric 項(xiàng)目后,想要利用 Entity Framework 的 CodeFrist 將數(shù)據(jù)插入數(shù)據(jù)庫(kù)時(shí)可能會(huì)遇到以下問題。
問題描述
使用 Visual studio 中的 nuget 管理將 Entity Framework 引入 Service Fabric 項(xiàng)目后,開啟 Enable-Migrations,會(huì)提示 Application 項(xiàng)目沒有安裝 EntityFramework。
當(dāng)我們嘗試在 Application 中引入 EntityFramework 時(shí)報(bào)錯(cuò):
Install-Package : Failed to add reference. The package 'EntityFramework' tried to add a framework reference to 'System.ComponentModel.DataAnnotations' which was not found in the GAC. This is possibly a bug in the package. Please contact the package owners for assistance.
解決方法
我們以 Stateless 類型的 Service Fabric 項(xiàng)目為例:
1.首先要確保在執(zhí)行 Enable-Migrations 的時(shí)候,設(shè)定的 Default project 為 stateless project (如下面的例子:MyStateless ):

該 Service Fabric 項(xiàng)目的結(jié)構(gòu)如下:

2.保證 stateless project (例如:MyStateless) 已被設(shè)置為 Single startup Project:

3.由于 Enable-Migrations 依賴于 MyDbContext Class,因此需要建立了一個(gè) xxx.cs 的文件,在這個(gè)文件中對(duì)該類進(jìn)行定義:

4.運(yùn)行 Enable-Migrations 命令時(shí)要指定 ContextTypeName 為之前 xxx.cs 文件中的 MyDbContext Class,例如:Enable-Migrations -contexttypename MileyClass.MyDbContext。
查看運(yùn)行結(jié)果可以點(diǎn)擊此處。