Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add: NullRef when adding graph with shadow keys #4854

Closed
bricelam opened this issue Mar 18, 2016 · 0 comments
Closed

Add: NullRef when adding graph with shadow keys #4854

bricelam opened this issue Mar 18, 2016 · 0 comments
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-bug
Milestone

Comments

@bricelam
Copy link
Contributor

class TestAssembly
{
    [Key]
    public string Name { get; set; }
    public ICollection<TestClass> Classes { get; } = new List<TestClass>();
}

class TestClass
{
    public TestAssembly Assembly { get; set; }
    public string Name { get; set; }
    public ICollection<Test> Tests { get; } = new List<Test>();
}

class Test
{
    public TestClass Class { get; set; }
    public string Name { get; set; }
}

class Context : DbContext
{
    public DbSet<TestAssembly> Assemblies { get; set; }
    public DbSet<TestClass> Classes { get; set; }
    public DbSet<Test> Tests { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder options)
        => options.UseSqlite("Data Source=ConsoleApplication1.db");

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<TestClass>(
            x =>
            {
                x.Property<string>("AssemblyName");
                x.HasKey("AssemblyName", nameof(TestClass.Name));
                x.HasOne(c => c.Assembly).WithMany(a => a.Classes)
                    .HasForeignKey("AssemblyName");
            });
        modelBuilder.Entity<Test>(
            x =>
            {
                x.Property<string>("AssemblyName");
                x.HasKey("AssemblyName", "ClassName", nameof(Test.Name));
                x.HasOne(t => t.Class).WithMany(c => c.Tests)
                    .HasForeignKey("AssemblyName", "ClassName");
            });
    }
}

class Program
{
    static void Main()
    {
        using (var db = new Context())
        {
            db.Database.EnsureDeleted();
            db.Database.EnsureCreated();

            var assembly = new TestAssembly { Name = "Assembly1" };
            var testClass = new TestClass { Assembly = assembly, Name = "Class1" };
            db.Tests.Add(new Test { Class = testClass, Name = "Test1" });

            // Throws NullReferenceException
            db.SaveChanges();
        }
    }
}
System.NullReferenceException: Object reference not set to an instance of an object.
    at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.CompositePrincipalKeyValueFactory.CompositeComparer.Equals(Object[] x, Object[] y)
    at System.Collections.Generic.Dictionary`2.Remove(TKey key)
    at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.IdentityMap`1.Remove(TKey key, InternalEntityEntry entry)
    at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.NullableKeyIdentityMap`1.RemoveUsingRelationshipSnapshot(InternalEntityEntry entry)
    at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.UpdateIdentityMap(InternalEntityEntry entry, IKey key)
    at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.NavigationFixer.KeyPropertyChanged(InternalEntityEntry entry, IProperty property, IReadOnlyList`1 containingPrincipalKeys, IReadOnlyList`1 containingForeignKeys, Object oldValue, Object newValue)
    at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntryNotifier.<>c__DisplayClass9_0.<KeyPropertyChanged>b__0(IKeyListener l)
    at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntryNotifier.Dispatch(Action`1 action)
    at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntryNotifier.KeyPropertyChanged(InternalEntityEntry entry, IProperty property, IReadOnlyList`1 keys, IReadOnlyList`1 foreignKeys, Object oldValue, Object newValue)
    at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.ChangeDetector.DetectKeyChange(InternalEntityEntry entry, IProperty property)
    at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.ChangeDetector.PropertyChanged(InternalEntityEntry entry, IPropertyBase propertyBase, Boolean setModified)
    at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntryNotifier.<>c__DisplayClass10_0.<PropertyChanged>b__0(IPropertyListener l)
    at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntryNotifier.Dispatch(Action`1 action)
    at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntryNotifier.PropertyChanged(InternalEntityEntry entry, IPropertyBase property, Boolean setModified)
    at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry.SetProperty(IPropertyBase propertyBase, Object value, Boolean setModified)
    at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.NavigationFixer.SetForeignKeyProperties(InternalEntityEntry dependentEntry, InternalEntityEntry principalEntry, IForeignKey foreignKey, Boolean setModified)
    at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.NavigationFixer.FixupToPrincipal(InternalEntityEntry dependentEntry, InternalEntityEntry principalEntry, IForeignKey foreignKey, Boolean setModified)
    at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.NavigationFixer.DelayedFixup(InternalEntityEntry entry, INavigation navigation, InternalEntityEntry referencedEntry)
    at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.NavigationFixer.InitialFixup(InternalEntityEntry entry, Boolean fromQuery)
    at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.NavigationFixer.StateChanged(InternalEntityEntry entry, EntityState oldState, Boolean skipInitialFixup, Boolean fromQuery)
    at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntryNotifier.<>c__DisplayClass6_0.<StateChanged>b__0(IEntityStateListener l)
    at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntryNotifier.Dispatch(Action`1 action)
    at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntryNotifier.StateChanged(InternalEntityEntry entry, EntityState oldState, Boolean skipInitialFixup, Boolean fromQuery)
    at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry.SetEntityState(EntityState oldState, EntityState newState, Boolean acceptChanges)
    at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry.SetEntityState(EntityState entityState, Boolean acceptChanges)
    at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.EntityGraphAttacher.PaintAction(EntityEntryGraphNode node)
    at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.EntityEntryGraphIterator.TraverseGraph(EntityEntryGraphNode node, Func`2 handleNode)
    at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.EntityEntryGraphIterator.TraverseGraph(EntityEntryGraphNode node, Func`2 handleNode)
    at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.EntityGraphAttacher.AttachGraph(InternalEntityEntry rootEntry, EntityState entityState)
    at Microsoft.EntityFrameworkCore.DbContext.SetEntityState(InternalEntityEntry entry, EntityState entityState)
    at Microsoft.EntityFrameworkCore.DbContext.SetEntityState[TEntity](TEntity entity, EntityState entityState)
    at Microsoft.EntityFrameworkCore.DbContext.Add[TEntity](TEntity entity)
    at Microsoft.EntityFrameworkCore.Internal.InternalDbSet`1.Add(TEntity entity)
    at ConsoleApplication1.Program.Main()
    at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
    at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
    at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
    at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
    at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
    at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
    at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
    at System.Threading.ThreadHelper.ThreadStart()
@rowanmiller rowanmiller added this to the 1.0.0 milestone Mar 25, 2016
ajcvickers added a commit that referenced this issue Apr 27, 2016
Issue #4854

Switch to using Equals(x, y) instead of x.Equals(y).
ajcvickers added a commit that referenced this issue Apr 27, 2016
Issue #4854

Switch to using Equals(x, y) instead of x.Equals(y).
@ajcvickers ajcvickers added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Oct 15, 2022
@ajcvickers ajcvickers removed their assignment Sep 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-bug
Projects
None yet
Development

No branches or pull requests

3 participants