今天使用了一下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包應該也能解決問題,不過我就不去試了哈哈哈哈哈