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

Fix adding duplicate entities #174

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified GraphDiff/.vs/GraphDiff/v15/sqlite3/storage.ide
Binary file not shown.
1 change: 1 addition & 0 deletions GraphDiff/GraphDiff.Tests/GraphDiff.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Bootstrapper.cs" />
<Compile Include="Models\MyModels.cs" />
<Compile Include="Models\TestModels.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="TestDbContext.cs" />
Expand Down
62 changes: 62 additions & 0 deletions GraphDiff/GraphDiff.Tests/Models/ModelsWithDublicateEntities.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace RefactorThis.GraphDiff.Tests.Models
{
public class ModelRoot
{
public Guid Id { get; set; }
public virtual ICollection<ModelLevel1> MyModelsLevel1 { get; set; }
}

public class ModelLevel1
{
protected bool Equals(ModelLevel1 other)
{
return Id.Equals(other.Id);
}

public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != this.GetType()) return false;
return Equals((ModelLevel1) obj);
}

public override int GetHashCode()
{
return Id.GetHashCode();
}

public Guid Id { get; set; }

public virtual ModelLevel2 ModelLevel2 { get; set; }
}

public class ModelLevel2
{
protected bool Equals(ModelLevel2 other)
{
return Code.Equals(other.Code);
}

public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != this.GetType()) return false;
return Equals((ModelLevel2) obj);
}

public override int GetHashCode()
{
return Code.GetHashCode();
}

public Guid Code { get; set; }
public string Name { get; set; }
}
}
4 changes: 4 additions & 0 deletions GraphDiff/GraphDiff.Tests/TestDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ protected override void OnModelCreating(DbModelBuilder modelBuilder)
modelBuilder.Entity<InternalKeyModel>()
.HasMany(ikm => ikm.Associates)
.WithRequired(ikm => ikm.Parent);

modelBuilder.Entity<ModelRoot>().HasKey(x => x.Id).HasMany(x => x.MyModelsLevel1);
modelBuilder.Entity<ModelLevel1>().HasKey(x => x.Id).HasOptional(x => x.ModelLevel2);
modelBuilder.Entity<ModelLevel2>().HasKey(x => x.Code);
}

public TestDbContext() : base("GraphDiff") {}
Expand Down
69 changes: 66 additions & 3 deletions GraphDiff/GraphDiff.Tests/Tests/AddAggregateBehaviours.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Data.Entity;
using System;
using System.Data.Entity;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using RefactorThis.GraphDiff.Tests.Models;
using System.Linq;
Expand All @@ -9,6 +10,68 @@ namespace RefactorThis.GraphDiff.Tests.Tests
[TestClass]
public class AddAggregateBehaviours : TestBase
{
[TestMethod]
public void ShouldAddNewAggregateRootWithDuplicateEntityOnLevel1()
{
var guid = Guid.Parse("C1775DA8-61EC-47A1-ADF9-A50653820061");

var node1 = new ModelRoot()
{
Id = Guid.NewGuid(),
MyModelsLevel1 = new List<ModelLevel1>()
{
new ModelLevel1() {Id = guid},
new ModelLevel1() {Id = guid}
}
};

using (var context = new TestDbContext())
{
node1 = context.UpdateGraph(node1, map =>
map.OwnedCollection(p => p.MyModelsLevel1,
with => with.OwnedEntity(p => p.ModelLevel2)));

context.SaveChanges();
}

using (var context = new TestDbContext())
{
var model = context.Set<ModelRoot>().Include(x => x.MyModelsLevel1).FirstOrDefault();
Assert.IsTrue(model.MyModelsLevel1.All(x => x.Id == guid));
}
}

[TestMethod]
public void ShouldAddNewAggregateRootWithduplicateEntityOnLevel2()
{
var guid = Guid.Parse("C1775DA8-61EC-47A1-ADF9-A50653820061");

var node1 = new ModelRoot()
{
Id = Guid.NewGuid(),
MyModelsLevel1 = new List<ModelLevel1>()
{
new ModelLevel1() {Id = Guid.NewGuid(), ModelLevel2 = new ModelLevel2() {Code = guid}},
new ModelLevel1() {Id = Guid.NewGuid(), ModelLevel2 = new ModelLevel2() {Code = guid}}
}
};

using (var context = new TestDbContext())
{
node1 = context.UpdateGraph(node1, map =>
map.OwnedCollection(p => p.MyModelsLevel1,
with => with.OwnedEntity(p => p.ModelLevel2)));

context.SaveChanges();
}

using (var context = new TestDbContext())
{
var models = context.Set<ModelLevel1>().Include(x => x.ModelLevel2).ToList();
Assert.IsTrue(models.All(x => x.ModelLevel2.Code == guid));
}
}

[TestMethod]
public void ShouldAddNewAggregateRoot_Detached()
{
Expand All @@ -19,7 +82,7 @@ public void ShouldAddNewAggregateRoot_Detached()
Title = "New Node",
OneToManyOwned = new List<OneToManyOwnedModel>
{
new OneToManyOwnedModel { Title = "One" },
new OneToManyOwnedModel { Title = "One"},
new OneToManyOwnedModel { Title = "Two" },
new OneToManyOwnedModel { Title = "Three" }
},
Expand All @@ -44,7 +107,7 @@ public void ShouldAddNewAggregateRoot_Detached()
node1 = context.UpdateGraph(node1, map => map
.OwnedEntity(p => p.OneToOneOwned)
.AssociatedEntity(p => p.OneToOneAssociated)
.OwnedCollection(p => p.OneToManyOwned)
.OwnedCollection(p => p.OneToManyOwned, with => with.OwnedEntity(p => p.OneToManyOneToOneOwned))
.AssociatedCollection(p => p.OneToManyAssociated));

context.SaveChanges();
Expand Down
6 changes: 3 additions & 3 deletions GraphDiff/GraphDiff/GraphDiff.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,11 @@
<DocumentationFile>bin\Release\RefactorThis.GraphDiff.xml</DocumentationFile>
</PropertyGroup>
<PropertyGroup>
<SignAssembly>true</SignAssembly>
<SignAssembly>false</SignAssembly>
</PropertyGroup>
<PropertyGroup>
<AssemblyOriginatorKeyFile>zzzproject.pfx</AssemblyOriginatorKeyFile>
<AssemblyOriginatorKeyFile>
</AssemblyOriginatorKeyFile>
</PropertyGroup>
<ItemGroup>
<Reference Include="EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL">
Expand Down Expand Up @@ -73,7 +74,6 @@
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
<None Include="zzzproject.pfx" />
</ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
Expand Down
16 changes: 10 additions & 6 deletions GraphDiff/GraphDiff/Internal/Graph/CollectionGraphNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,18 +64,22 @@ private object AddElement<T>(DbContext context, T existing, object updateItem, o
var entityType = ObjectContext.GetObjectType(updateItem.GetType());
var instance = CreateEmptyEntityWithKey(context, updateItem);

context.Set(entityType).Add(instance);
context.Entry(instance).CurrentValues.SetValues(updateItem);

foreach (var childMember in Members)
if (!(bool) dbCollection.GetType().GetMethod("Contains").Invoke(dbCollection, new[] {updateItem}))
{
childMember.Update(context, instance, updateItem);
context.Set(entityType).Add(instance);
context.Entry(instance).CurrentValues.SetValues(updateItem);

foreach (var childMember in Members)
{
childMember.Update(context, instance, updateItem);
}
}

updateItem = instance;
}

dbCollection.GetType().GetMethod("Add").Invoke(dbCollection, new[] {updateItem});
var contains = (bool)dbCollection.GetType().GetMethod("Contains").Invoke(dbCollection, new[] { updateItem });
if (!contains) dbCollection.GetType().GetMethod("Add").Invoke(dbCollection, new[] { updateItem });

AttachCyclicNavigationProperty(context, existing, updateItem);

Expand Down
20 changes: 18 additions & 2 deletions GraphDiff/GraphDiff/Internal/Graph/OwnedEntityGraphNode.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Data.Entity;
using System.Linq;
using System.Reflection;

namespace RefactorThis.GraphDiff.Internal.Graph
Expand Down Expand Up @@ -36,14 +37,29 @@ public override void Update<T>(DbContext context, T persisted, T updating)

AttachCyclicNavigationProperty(context, persisted, newValue);

foreach (var childMember in Members)
childMember.Update(context, dbValue, newValue);
if (dbValue != null)
{
foreach (var childMember in Members)
childMember.Update(context, dbValue, newValue);
}
}

private object CreateNewPersistedEntity<T>(DbContext context, T existing, object newValue) where T : class, new()
{
var local = context.Set(Accessor.PropertyType).Local;
foreach (var entity in local)
{
if (entity.Equals(newValue))
{
SetValue(existing, entity);
return entity;
}
}

var instance = Activator.CreateInstance(newValue.GetType());
SetValue(existing, instance);


context.Set(Accessor.PropertyType).Add(instance);
UpdateValuesWithConcurrencyCheck(context, newValue, instance);
return instance;
Expand Down
4 changes: 2 additions & 2 deletions GraphDiff/RefactorThis.GraphDiff.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
<package xmlns="http://schemas.microsoft.com/packaging/2011/08/nuspec.xsd">
<metadata>
<id>RefactorThis.GraphDiff</id>
<version>2.0.1</version>
<version>3.0.5</version>
<title>GraphDiff - Entity Framework graph auto-update</title>
<authors>brentmckendrick</authors>
<owners>brentmckendrick</owners>
<projectUrl>https://github.com/refactorthis/GraphDiff</projectUrl>
<iconUrl>http://s8.postimg.org/r3f1stx69/Graph_Diff.png</iconUrl>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>This is the official GraphDiff package.
<description>This is the fork of GraphDiff package.

GraphDiff - Allows for automatic update of a graph of detached entities using Entity Framework v6+.

Expand Down