【发布时间】:2021-07-26 13:49:30
【问题描述】:
我有 ASP.Net Core 项目,作为 ORM,它基于 Entity Framework Core 版本 3.1.8。它可以迁移和更新,以便从 CLor 包管理器控制台进行首次初始创建,不用担心。问题是当我为现有实体添加新表或新属性时,无法添加新迁移。异常详情如下。
PM> dotnet ef 迁移添加“newone”
构建开始...
构建成功。
System.NullReferenceException:对象引用未设置为对象的实例。
在 Microsoft.EntityFrameworkCore.Update.Internal.SharedTableEntryMap1.GetMainEntry(IUpdateEntry entry) at Microsoft.EntityFrameworkCore.Update.Internal.SharedTableEntryMap1.GetOrAddValue(IUpdateEntry 条目)
在 Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.DiffData(TableMapping 源、TableMapping 目标、DiffContext diffContext)
在 Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.Diff(TableMapping 源,TableMapping 目标,DiffContext diffContext)+MoveNext()
在 Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.DiffCollection[T](IEnumerable1 sources, IEnumerable1 目标,DiffContext diffContext,Func4 diff, Func3 添加,Func3 remove, Func4[] 谓词)+MoveNext()
在 System.Linq.Enumerable.ConcatIterator1.MoveNext() at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.Sort(IEnumerable1 操作,DiffContext diffContext)
在 Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.GetDifferences(IModel 源,IModel 目标)
在 Microsoft.EntityFrameworkCore.Migrations.Design.MigrationsScaffolder.ScaffoldMigration(字符串迁移名称、字符串根命名空间、字符串子命名空间、字符串语言)
在 Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.AddMigration(字符串名称,字符串 outputDir,字符串 contextType)
在 Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigrationImpl(字符串名称,字符串 outputDir,字符串 contextType)
在 Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigration.c__DisplayClass0_0.<.ctor>b__0()
在 Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.c__DisplayClass3_0`1.b__0()
在 Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(操作动作)
对象引用未设置为对象的实例。
我的 DbContext 和 DbContextFactory 代码 sn-ps 也在下面
//我的DbContextFactory CreateDbContext方法
public DataContext CreateDbContext(string[] args)
{
var env = GetEnvironment();
var connectionStr = SetDatabaseConnectionString(env);
var optionsBuilder = new DbContextOptionsBuilder<DataContext>();
optionsBuilder.UseSqlServer(Environment.GetEnvironmentVariable(connectionStr, EnvironmentVariableTarget.Machine));
optionsBuilder.EnableSensitiveDataLogging();
return new DataContext(optionsBuilder.Options, null);
}
//我的 DbContext 构造函数
public DataContext(DbContextOptions<DataContext> options, IHttpContextAccessor httpContextAccessor) : base(options)
{
_logger = LogManager.GetCurrentClassLogger();
_logger.Info("Domain User : SYSTEM Details : DataContext initialized");
Database?.SetCommandTimeout(5000);//TODO : fetch from config
_httpContextAccessor = httpContextAccessor;
}
【问题讨论】:
-
OnModelCreating中是否有使用_httpContextAccessor的代码?.HasData(...)来电? -
您好伊万,感谢您的回复。在 OnModelCreating 内部,未使用 _httpContextAccessor,但在 OnModelCreating 内部调用的每个实体都有种子函数
-
是的,其中一些种子函数的实现似乎引起了问题(生成空引用异常)。尝试将它们一一注释掉,直到异常消失。或者分析实现(进行一些健全性检查/
Debug.Assert或类似的东西)。这就是我从异常调用跟踪中所能猜到的全部 -DiffData调用是通过HasDatafluent API 获取数据种子。 -
感谢您的建议。我会写结果。
-
@IvanStoev 它花了很长时间,但我们很快就解决了。下面提到解决方案
标签: entity-framework-migrations entity-framework-core-3.1