Entity Framework Core Adding New Migration After Initial Creation

300 Views Asked by At

I have ASP.Net Core project and as ORM, it's based on Entity Framework Core version 3.1.8. It can be migrated and updated for first initial creation from CLor Package Manager Console, no worries. The problem is when I add a new table or a new property for existing entity, it's unable to add new migration. Exception details are below.

PM> dotnet ef migrations add "newone" Build started... Build succeeded. System.NullReferenceException: Object reference not set to an instance of an object. at Microsoft.EntityFrameworkCore.Update.Internal.SharedTableEntryMap1.GetMainEntry(IUpdateEntry entry) at Microsoft.EntityFrameworkCore.Update.Internal.SharedTableEntryMap1.GetOrAddValue(IUpdateEntry entry) at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.DiffData(TableMapping source, TableMapping target, DiffContext diffContext) at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.Diff(TableMapping source, TableMapping target, DiffContext diffContext)+MoveNext() at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.DiffCollection[T](IEnumerable1 sources, IEnumerable1 targets, DiffContext diffContext, Func4 diff, Func3 add, Func3 remove, Func4[] predicates)+MoveNext() at System.Linq.Enumerable.ConcatIterator1.MoveNext() at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.Sort(IEnumerable1 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.b__0() at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action) Object reference not set to an instance of an object.

Also my DbContext & DbContextFactory code snippets are below

//My DbContextFactory CreateDbContext Method

  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);

    }

//My DbContext Constructor

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;
    }
1

There are 1 best solutions below

0
On BEST ANSWER

After lots of staff the problem is solved. It was, there is navigation property exists in entity but relation was not defined. After trying to add a new migration, null object reference exception was thrown but no details! I debugged all migration class, and at last realized the null object was the relation. I just advice if you face any kind of migration problem, comment all poco entities and check all entity navigations and relations step by step to find the problem.