Skip to content

Commit c029f27

Browse files
author
Andreas Pelzer
committed
Merge pull request #64 from andypelzer/issue62
Test & Fix for issue #62
2 parents 1c6b0c6 + a93809a commit c029f27

File tree

4 files changed

+63
-6
lines changed

4 files changed

+63
-6
lines changed

GraphDiff/GraphDiff.Tests/Models/TestModels.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,16 @@ public class InternalKeyModel
9393
{
9494
[Key]
9595
internal int Id { get; set; }
96+
97+
internal List<InternalKeyAssociate> Associates { get; set; }
98+
}
99+
100+
public class InternalKeyAssociate
101+
{
102+
[Key]
103+
internal int Id { get; set; }
104+
105+
internal InternalKeyModel Parent { get; set; }
96106
}
97107

98108
public class NullableKeyModel

GraphDiff/GraphDiff.Tests/TestDbContext.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,6 @@ public class TestDbContext : DbContext
2727

2828
protected override void OnModelCreating(DbModelBuilder modelBuilder)
2929
{
30-
// misc
31-
32-
modelBuilder.Entity<InternalKeyModel>().HasKey(i => i.Id);
33-
3430
// second tier mappings
3531

3632
modelBuilder.Entity<TestNode>().HasOptional(p => p.OneToOneAssociated).WithOptionalPrincipal(p => p.OneParent);
@@ -57,6 +53,15 @@ protected override void OnModelCreating(DbModelBuilder modelBuilder)
5753
// Guid mappings
5854

5955
modelBuilder.Entity<GuidTestNode>().HasOptional(p => p.OneToOneOwned).WithRequired(p => p.OneParent);
56+
57+
// Internal mappings
58+
59+
modelBuilder.Entity<InternalKeyModel>().HasKey(i => i.Id);
60+
modelBuilder.Entity<InternalKeyAssociate>().HasKey(i => i.Id);
61+
62+
modelBuilder.Entity<InternalKeyModel>()
63+
.HasMany(ikm => ikm.Associates)
64+
.WithRequired(ikm => ikm.Parent);
6065
}
6166

6267
public TestDbContext() : base("GraphDiff") {}

GraphDiff/GraphDiff.Tests/Tests/MiscBehaviours.cs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
using System;
2+
using System.Collections.Generic;
3+
using System.Data.Entity;
4+
using System.Linq;
25
using Microsoft.VisualStudio.TestTools.UnitTesting;
36
using RefactorThis.GraphDiff.Tests.Models;
47

@@ -47,6 +50,45 @@ public void ShouldSupportInternalKeys()
4750
}
4851
}
4952

53+
[TestMethod]
54+
public void ShouldSupportInternalNavigationProperties()
55+
{
56+
var parent = new InternalKeyModel();
57+
using (var context = new TestDbContext())
58+
{
59+
context.InternalKeyModels.Add(parent);
60+
context.SaveChanges();
61+
} // simulate detach
62+
63+
parent.Associates = new List<InternalKeyAssociate> { new InternalKeyAssociate() };
64+
65+
InternalKeyModel model;
66+
using (var context = new TestDbContext())
67+
{
68+
model = context.UpdateGraph(parent, map => map.AssociatedCollection(ikm => ikm.Associates));
69+
context.SaveChanges();
70+
71+
Assert.AreNotEqual(0, model.Id);
72+
73+
Assert.IsNotNull(model.Associates);
74+
Assert.AreEqual(1, model.Associates.Count);
75+
Assert.AreNotEqual(0, model.Associates.First().Id);
76+
}
77+
78+
using (var context = new TestDbContext())
79+
{
80+
var reloadedModel = context.InternalKeyModels
81+
.Include(ikm => ikm.Associates)
82+
.SingleOrDefault(ikm => ikm.Id == model.Id);
83+
84+
Assert.IsNotNull(reloadedModel);
85+
Assert.IsNotNull(reloadedModel.Associates);
86+
87+
Assert.AreEqual(1, reloadedModel.Associates.Count);
88+
Assert.AreEqual(model.Associates.Single().Id, reloadedModel.Associates.Single().Id);
89+
}
90+
}
91+
5092
[TestMethod]
5193
public void ShouldSupportNullableKeys()
5294
{

GraphDiff/GraphDiff/Internal/Graph/GraphNode.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ protected static void AttachRequiredNavigationProperties(DbContext context, obje
167167
var entityType = ObjectContext.GetObjectType(updating.GetType());
168168
foreach (var navigationProperty in context.GetRequiredNavigationPropertiesForType(updating.GetType()))
169169
{
170-
var navigationPropertyInfo = entityType.GetProperty(navigationProperty.Name);
170+
var navigationPropertyInfo = entityType.GetProperty(navigationProperty.Name, BindingFlags.Instance | BindingFlags.NonPublic);
171171
var associatedEntity = navigationPropertyInfo.GetValue(updating, null);
172172

173173
if (associatedEntity != null)
@@ -224,7 +224,7 @@ private static void EnsureConcurrency<T>(IObjectContextAdapter db, T entity1, T
224224
var conceptualType = metadata.GetItems<EntityType>(DataSpace.CSpace).Single(p => p.FullName == cTypeName);
225225
var concurrencyProperties = conceptualType.Members
226226
.Where(member => member.TypeUsage.Facets.Any(facet => facet.Name == "ConcurrencyMode" && (ConcurrencyMode)facet.Value == ConcurrencyMode.Fixed))
227-
.Select(member => entityType.GetProperty(member.Name))
227+
.Select(member => entityType.GetProperty(member.Name, BindingFlags.Instance | BindingFlags.NonPublic))
228228
.ToList();
229229

230230
// Check if concurrency properties are equal

0 commit comments

Comments
 (0)