如何调试程序包管理器控制台执行的ef核心代码?

时间:2018-12-05 09:17:15

标签: entity-framework-core entity ef-core-2.1 ef-core-2.2

我需要调试PM> add-migration <name>,该怎么做?

背景: 它引发以下错误。我想找到谁的原因

System.ArgumentNullException: Value cannot be null.
Parameter name: derivedType
   at Microsoft.EntityFrameworkCore.Utilities.Check.NotNull[T](T value, String parameterName)
   at Microsoft.EntityFrameworkCore.EntityTypeExtensions.IsAssignableFrom(IEntityType entityType, IEntityType derivedType)
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.<>c__DisplayClass55_1.<StopTracking>b__0(INavigation n)
   at System.Linq.Enumerable.Any[TSource](IEnumerable`1 source, Func`2 predicate)
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.StopTracking(InternalEntityEntry entry)
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry.SetEntityState(EntityState oldState, EntityState newState, Boolean acceptChanges)
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry.SetEntityState(EntityState entityState, Boolean acceptChanges, Nullable`1 forceStateWhenUnknownKey)
   at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.GetDataOperations()+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_1.<.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)
Value cannot be null.

1 个答案:

答案 0 :(得分:0)

连接字符串为null时最常见的错误。

这不是您想要的,但是无论如何,它可以为您提供帮助。尝试记录错误或调试信息:

    public class Startup
    {
        private ILogger<Startup> Logger { get; }    
        public Startup(ILogger<Startup> logger)
        {
            Logger = logger;
        }    

        public void ConfigureServices(IServiceCollection services)
        {
            Logger.LogError("test");  <========= It will output messages


            string connection = null;
            services.AddDbContext<TestContext>(builder => builder.UseSqlServer(connection));

            ...
        }
        ...
    }