private void DetectChanges_for_changing_fks_that_are_part_of_pk(CompositeBlog663 blog) { var modelBuilder = new DbModelBuilder(); modelBuilder .Entity <CompositeBlog663>() .HasKey(e => new { e.Id1, e.Id2 }) .HasMany(e => e.Posts) .WithOptional(e => e.Blog) .HasForeignKey(e => new { e.BlogId1, e.BlogId2 }); modelBuilder.Entity <CompositePost663>() .HasKey(e => new { e.Id, e.BlogId2 }); using (var context = new DbContext("Context2628", modelBuilder.Build(ProviderRegistry.Sql2008_ProviderInfo).Compile())) { context.Configuration.AutoDetectChangesEnabled = false; context.Database.Initialize(force: false); context.Set <CompositeBlog663>().Add(blog); var post = context.Set <CompositePost663>().Add(new CompositePost663()); post.Id = 1; post.BlogId1 = 1; post.BlogId2 = 1; Assert.Equal(EntityState.Added, context.Entry(blog).State); Assert.Equal(EntityState.Added, context.Entry(post).State); ((IObjectContextAdapter)context).ObjectContext.AcceptAllChanges(); post.Blog = blog; context.ChangeTracker.DetectChanges(); Assert.Equal(EntityState.Unchanged, context.Entry(blog).State); Assert.Same(post, blog.Posts.First()); Assert.Equal(EntityState.Modified, context.Entry(post).State); Assert.Equal(blog.Id1, post.BlogId1); Assert.Equal(blog.Id2, post.BlogId2); } }
private void DetectChanges_for_changing_fks_that_are_part_of_pk(CompositeBlog663 blog) { var modelBuilder = new DbModelBuilder(); modelBuilder .Entity<CompositeBlog663>() .HasKey(e => new { e.Id1, e.Id2 }) .HasMany(e => e.Posts) .WithOptional(e => e.Blog) .HasForeignKey(e => new { e.BlogId1, e.BlogId2 }); modelBuilder.Entity<CompositePost663>() .HasKey(e => new { e.Id, e.BlogId2 }); using (var context = new DbContext("Context2628", modelBuilder.Build(ProviderRegistry.Sql2008_ProviderInfo).Compile())) { context.Configuration.AutoDetectChangesEnabled = false; context.Database.Initialize(force: false); context.Set<CompositeBlog663>().Add(blog); var post = context.Set<CompositePost663>().Add(new CompositePost663()); post.Id = 1; post.BlogId1 = 1; post.BlogId2 = 1; Assert.Equal(EntityState.Added, context.Entry(blog).State); Assert.Equal(EntityState.Added, context.Entry(post).State); ((IObjectContextAdapter)context).ObjectContext.AcceptAllChanges(); post.Blog = blog; context.ChangeTracker.DetectChanges(); Assert.Equal(EntityState.Unchanged, context.Entry(blog).State); Assert.Same(post, blog.Posts.First()); Assert.Equal(EntityState.Modified, context.Entry(post).State); Assert.Equal(blog.Id1, post.BlogId1); Assert.Equal(blog.Id2, post.BlogId2); } }