efcore: Add-Migration with changing FKs fails in 2.1 (works again in 2.2 preview)

I’m leaving this here as a breadcrumb, should anyone else hit this problem. I originally brought it up in https://github.com/aspnet/EntityFrameworkCore/issues/329 because I thought it was related to SQLite table rebuilds due to constraint changes.

Overview I have a solution with a number of custom mappings. Along the way I did two mapping changes that were fine in 2.0.0-2.0.3, failed in 2.1.0-2.1.4 and succeed again with the current 2.2 preview.

This happens with the SQLite provider as well as the SQL Server provider.

When I recreated a much simpler repro there was no problem, so it is specific to something else in my model. I will put a repro with MY model on my github account if you are interested in debugging against the EF Core code. (When I do that I will update this issue).

Scenario one: Adding a join entity and mapping for a Many-to-Many The first mapping change creates a many to many relationship between two entities: Manager and Team That means adding ManagerTeamHistory class

public class ManagerTeamHistory {
    public ManagerTeamHistory (Guid managerId, Guid teamId) {
      ManagerId = managerId;
      TeamId = teamId;
    }
    public Guid ManagerId { get; private set; }
    public Guid TeamId { get; private set; }
  }

There’s a navigation property in only ONE end, which satisfies my domain logic.

I added the mapping in the context modelBuilder.Entity<ManagerTeamHistory> ().HasKey (m => new { m.ManagerId, m.TeamId });
And then tried to add the new migration which failed with this error:

data git:(TEST_E1) ✗ dotnet ef migrations add init -s ../console
System.NullReferenceException: Object reference not set to an instance of an object.
  at Microsoft.EntityFrameworkCore.Update.Internal.SharedTableEntryMap`1.CreateSharedTableEntryMapFactory(IReadOnlyList`1 entityTypes, IStateManager stateManager, String tableName, String schema)
  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](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.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.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.

Scenario Two: Replacing a public property in a one-to-one relationship with a backing field I had the same error after encapsulating a property in a one to one relationship. Changing public Manager Manager { get; set; } to

private Manager _manager;
private Manager Manager =>_manager ;
public string ManagerName=>_manager.Name;

Because this is resolved in 2.2 as mysteriously as it appeared in 2.1, I am not very concerned about it but wanted to leave this here in case anyone was interested.

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 20 (4 by maintainers)

Most upvoted comments

We no longer get this error with 2.2