question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Related entities in collection duplicate after inserting an object graph to database

See original GitHub issue

Issue: after inserting a new entity, that contains another new related entities in its collection properties (one-to-many relationship), to a database - every related entity duplicates in the corresponding collection.

I’m using ABP 3.4.0, .net core 2.0, EF Core, Sql Server 2016 LocalDB.

Steps I took to reproduce an issue:

  • Went to ABP Download Template page
  • Chose following settings (I chose ‘Multi Page Web Application’, but I guess it doesn’t matter):

image

  • Set connection string to Data Source=(localdb)\MSSQLLocalDB; Database=testABPDb; Trusted_Connection=True;
  • Added two simplest entities to Core project:
public class ProductTest : Entity<int>
{
	public ProductTest()
	{
		ProductImages = new HashSet<ProductImageTest>();
	}

	[ForeignKey("ProductId")]
	public virtual ICollection<ProductImageTest> ProductImages { get; set; }
}

public class ProductImageTest : Entity<int>
{
	[ForeignKey(nameof(Product))]
	[Required]
	public int ProductId { get; set; }

	public virtual ProductTest Product { get; set; }
}
  • Added them to DbContext:
public class testABPDbContext : AbpZeroDbContext<Tenant, Role, User, testABPDbContext>
{
	/* Define a DbSet for each entity of the application */

	public virtual DbSet<ProductTest> ProductTest { get; set; }

	public virtual DbSet<ProductImageTest> ProductImageTests { get; set; }

	public testABPDbContext(DbContextOptions<testABPDbContext> options)
		: base(options)
	{
	}
}
  • Ran Add-Migration & Update-Database
  • Added simplest AppService:
public class TestAppService : testABPAppServiceBase
{
	private readonly IRepository<ProductTest> _repo;
	public TestAppService(IRepository<ProductTest> repository)
		
	{
		_repo = repository;
	}

	public string Test()
	{
		var product = new ProductTest();
		product.ProductImages.Add(new ProductImageTest { Product = product });

		// inserting product to database (whether with InsertAndGetId() or Insert()) causes 
		// every related ProductImageTest in collection to duplicate, i.e. product.ProductImages.Count() doubles
		_repo.Insert(product); 
		
		return product.ProductImages.Count.ToString(); // returns 2
	}
}
  • Launched Web.Host project, requested Test method() and got:

image

Although there is no duplication in the database (one record in ProductTest table and one in ProductImageTest table - as expected), Test() method returns 2 instead of 1, as there are two equivalent entities in product.ProductImages collection.

At first I thought there’s something wrong with my code, but I couldn’t find a bug at my end. I tested the same scenario without ABP (just pure EF Core, same entities) - and there was no duplication after inserting entities to database.

Am I missing something simple or is it a bug?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:11 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
douglasg14bcommented, Aug 23, 2019

I figured it out, it’s essentially this: https://github.com/aspnet/EntityFrameworkCore/issues/15687

0reactions
douglasg14bcommented, Nov 13, 2019

@luksfk I changed my GetHashCode to not use FK’s that may mutate and just compare by value, which works well for the aggregates I have.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to handle duplicates in disconnected object graph?
I'm having a problem updating a disconnected POCO model in an ASP.NET application. Lets say we have the following model: ... A user...
Read more >
Data Points - Why Does Entity Framework Reinsert Existing ...
The reason it happens is that when you use the DbSet.Add method (that is, Screencasts.Add), not only is the state of the root...
Read more >
HnD | Duplicate Record/Entity, Page 1
I need to duplicate the ENTIRE object graph - all related entities, etc. There are 1:1 relationships and 1:n relationships and I need...
Read more >
Identity Resolution - EF Core
These duplicates need to be resolved into single instances before the graph can be tracked.
Read more >
Entity deduplication in big data graphs for scholarly ...
Graphs are big when duplicate identification over the objects of such entity types require parallel-oriented approaches to scale up and ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found