Entity Framework Core遇到的問題匯總


1.add-migration : 無法將“add-migration”項識別為 cmdlet、函數、腳本文件或可運行程序的名稱。請檢查名稱的拼寫,如果包括路徑,請確保路徑正確,然后再試一次。

使用Entity Framework Core命令 add-migration manage 出現以下錯誤

PM> add-migration manage
add-migration : 無法將“add-migration”項識別為 cmdlet、函數、腳本文件或可運行程序的名稱。請檢查名稱的拼寫,如果包括路徑,請確保路徑正確,然后再試一次。
所在位置 行:1 字符: 1
+ add-migration manage
+ ~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (add-migration:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
 

 

使用EF core命令需要安裝遷移用的庫 Microsoft.EntityFrameworkCore.Tools ,所以在nuget 添加 Microsoft.EntityFrameworkCore.Tools包 或者 輸入 Install-Package Microsoft.EntityFrameworkCore.Tools

 

2.System.NullReferenceException: Object reference not set to an instance of an object.

刪除model中的一個屬性,然后提交 Add-Migration ,返回以下錯誤:

PM> Add-Migration ModifyCitys
Build started...
Build succeeded.
System.NullReferenceException: Object reference not set to an instance of an object.
   at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.Initialize(ColumnOperation columnOperation, IProperty property, CoreTypeMapping typeMapping, Boolean isNullable, IEnumerable`1 migrationsAnnotations, Boolean inline)
   at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.Add(IProperty target, DiffContext diffContext, Boolean inline)+MoveNext()
   at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.DiffCollection[T](IEnumerable`1 sources, IEnumerable`1 targets, DiffContext diffContext, Func`4 diff, Func`3 add, Func`3 remove, Func`4[] predicates)+MoveNext()
   at System.Linq.Enumerable.ConcatIterator`1.MoveNext()
   at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.Diff(ITable source, ITable target, DiffContext diffContext)+MoveNext()
   at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.DiffCollection[T](IEnumerable`1 sources, IEnumerable`1 targets, DiffContext diffContext, Func`4 diff, Func`3 add, Func`3 remove, Func`4[] predicates)+MoveNext()
   at System.Linq.Enumerable.ConcatIterator`1.MoveNext()
   at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.Sort(IEnumerable`1 operations, DiffContext diffContext)
   at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.GetDifferences(IModel source, IModel target)
   at Microsoft.EntityFrameworkCore.Migrations.Design.MigrationsScaffolder.ScaffoldMigration(String migrationName, String rootNamespace, String subNamespace, String language)
   at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.AddMigration(String name, String outputDir, String contextType)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigrationImpl(String name, String outputDir, String contextType)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigration.<>c__DisplayClass0_0.<.ctor>b__0()
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.<>c__DisplayClass3_0`1.<Execute>b__0()
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
Object reference not set to an instance of an object.

 

用的EF版本

EF Core version:5.0.0-preview.2.20120.8
Database provider: Microsoft.EntityFrameworkCore.SqlServer 5.0.0-preview.2.20120.8

3.1.2版本沒有問題,已提交issues

https://github.com/dotnet/efcore/issues/20348

 

3.More than one DbContext was found. Specify which one to use. Use the '-Context' parameter for PowerShell commands and the '--context' parameter for dotnet commands.

由於使用多個DbContext,在使用命令生成數據庫文件沒有指定DbContext

在原命令add-migration 后加上-c DbContext的類名

 

4.Unable to create an object of type 'MyDBContext'. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728

 

PM> Add-Migration Initial
Build started...
Build succeeded.
Unable to create an object of type 'MyDBContext'. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728

 

 

 

 

 

 

 

參考:

https://www.cnblogs.com/wyy1234/archive/2018/09/18/9670529.html

https://www.cnblogs.com/wyy1234/p/9647730.html

https://blog.csdn.net/xingkongtianyuzhao/article/details/104216879


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM