1. 程式人生 > >使用EF Core的CodeFirt 出現的問題The specified framework version '2.1' could not be parsed

使用EF Core的CodeFirt 出現的問題The specified framework version '2.1' could not be parsed

今天使用了一下EF Core的Code First,進行資料庫遷移的的時候報錯了:

The specified framework version '2.1' could not be parsed

解決方法:在DbContext的csporj工程檔案裡指定一下執行時框架

 <RuntimeFrameworkVersion>2.1.6</RuntimeFrameworkVersion>

新增完成後再次執行遷移指令:dotnet ef migrations add <記錄名>

結果又爆出了新的錯誤

Unable to create an object of type 'EFDbContext'. Add an implementation of 'IDesignTimeDbContextFactory<EFDbContext>' to the project, or see https://go.microsoft.com/fwlink/?linkid=851728 for additional patterns supported at design time.

出現這種錯誤是因為Dbcontext沒有在Web專案裡面,所以他找不到連結字串,沒辦法例項化DbContext

解決方案:我們在Dbcontext的同級目錄新建一個類DesignTimeDbContextFactory

讓這個類繼承IDesignTimeDbContextFactory介面

 

寫完後再次執行遷移指令,發現遷移成功

 

出現這種問題,我個人認為應該是我的DbContext沒有建到Web專案下的原因,我覺得如果引入一下Nuget的Asp.NETMVC包應該也能解決問題,不過我就不去試了哈哈哈哈哈