Related entities in collection duplicate after inserting an object graph to database
See original GitHub issueIssue: 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):
- 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:
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:
- Created 6 years ago
- Comments:11 (4 by maintainers)
Top GitHub Comments
I figured it out, it’s essentially this: https://github.com/aspnet/EntityFrameworkCore/issues/15687
@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.