From f804f150fb4482d5866bf3a9c6ed8ed5ce369290 Mon Sep 17 00:00:00 2001 From: Winslow Dalpe Date: Fri, 13 Feb 2015 09:54:49 -0800 Subject: [PATCH] Adding a unit test to demonstrate and detect issue #130. --- .../GraphDiff.Tests/Models/TestModels.cs | 18 +++++++++++++ GraphDiff/GraphDiff.Tests/TestDbContext.cs | 23 +++++++++------- .../Tests/OwnedEntityBehaviours.cs | 26 +++++++++++++++++++ 3 files changed, 58 insertions(+), 9 deletions(-) diff --git a/GraphDiff/GraphDiff.Tests/Models/TestModels.cs b/GraphDiff/GraphDiff.Tests/Models/TestModels.cs index 8988e6d..a92c84f 100644 --- a/GraphDiff/GraphDiff.Tests/Models/TestModels.cs +++ b/GraphDiff/GraphDiff.Tests/Models/TestModels.cs @@ -112,6 +112,24 @@ public class NullableKeyModel public Guid? Id { get; set; } } + public class PKFKModelPrimary + { + [Key] + [DatabaseGenerated(DatabaseGeneratedOption.Identity)] + public int? Id { get; set; } + + public PKFKModelSecondary Secondary { get; set; } + public string Name { get; set; } + } + + public class PKFKModelSecondary + { + [Key] + [DatabaseGenerated(DatabaseGeneratedOption.None)] + public int? Id { get; set; } + public string Name { get; set; } + } + // ==================================== // Second tier models // ==================================== diff --git a/GraphDiff/GraphDiff.Tests/TestDbContext.cs b/GraphDiff/GraphDiff.Tests/TestDbContext.cs index b8398cd..afb595b 100644 --- a/GraphDiff/GraphDiff.Tests/TestDbContext.cs +++ b/GraphDiff/GraphDiff.Tests/TestDbContext.cs @@ -4,8 +4,8 @@ namespace RefactorThis.GraphDiff.Tests { - public class TestDbContext : DbContext - { + public class TestDbContext : DbContext + { public IDbSet Nodes { get; set; } public IDbSet NodesWithReference { get; set; } @@ -16,7 +16,7 @@ public class TestDbContext : DbContext public IDbSet OneToManyAssociatedModels { get; set; } public IDbSet OneToManyOwnedModels { get; set; } - public IDbSet MultiKeyModels { get; set; } + public IDbSet MultiKeyModels { get; set; } public IDbSet RootEntities { get; set; } @@ -25,8 +25,11 @@ public class TestDbContext : DbContext public IDbSet InternalKeyModels { get; set; } public IDbSet NullableKeyModels { get; set; } - protected override void OnModelCreating(DbModelBuilder modelBuilder) - { + public IDbSet PKFKModelPrimaries { get; set; } + public IDbSet PKFKModelSecondaries { get; set; } + + protected override void OnModelCreating(DbModelBuilder modelBuilder) + { // second tier mappings modelBuilder.Entity().HasOptional(p => p.OneToOneAssociated).WithOptionalPrincipal(p => p.OneParent); @@ -61,9 +64,11 @@ protected override void OnModelCreating(DbModelBuilder modelBuilder) modelBuilder.Entity() .HasMany(ikm => ikm.Associates) - .WithRequired(ikm => ikm.Parent); - } + .WithRequired(ikm => ikm.Parent); + + modelBuilder.Entity().HasOptional(p => p.Secondary).WithRequired(); + } - public TestDbContext() : base("GraphDiff") {} - } + public TestDbContext() : base("GraphDiff") {} + } } diff --git a/GraphDiff/GraphDiff.Tests/Tests/OwnedEntityBehaviours.cs b/GraphDiff/GraphDiff.Tests/Tests/OwnedEntityBehaviours.cs index 08efd53..3738ee7 100644 --- a/GraphDiff/GraphDiff.Tests/Tests/OwnedEntityBehaviours.cs +++ b/GraphDiff/GraphDiff.Tests/Tests/OwnedEntityBehaviours.cs @@ -105,5 +105,31 @@ public void ShouldRemoveEntityIfRemovedFromParent() Assert.IsNull(context.OneToOneOwnedModels.SingleOrDefault(p => p.Id == oneToOne.Id)); } } + + [TestMethod] + public void ShouldCreateEntityWhenParentCreated_NullablePkFk() + { + var primary = new PKFKModelPrimary() + { + Name = "Primary", + Secondary = new PKFKModelSecondary() + { + Name = "Secondary" + } + }; + + PKFKModelPrimary persisted = null; + + using (var context = new TestDbContext()) + { + persisted = context.UpdateGraph(primary, mapping => mapping.OwnedEntity(p => p.Secondary)); + context.SaveChanges(); + } + + Assert.IsNotNull(persisted); + Assert.AreEqual(persisted.Id, persisted.Secondary.Id); + Assert.AreEqual(primary.Name, persisted.Name); + Assert.AreEqual(primary.Secondary.Name, persisted.Secondary.Name); + } } }