© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•4y ago•
3 replies
Bread

❔ EF Core duplicating many-to-many?

After saving, the references are duplicated!

        var entityShowcase = new Models.Showcase
        {
            AuthorId = showcase.AuthorId,
            Description = showcase.Description,
            Title = showcase.Description,
            Summary = showcase.Summary,
            ImageSource = showcase.ImageSource,
            MajorVersion = showcase.MajorVersion,
            MinorVersion = showcase.MinorVersion,
            PatchVersion = showcase.PatchVersion,
        };

        foreach (var feature in showcase.Features)
        {
            var dbFeatures = _context.Feature.FirstOrDefault(x => x.Value == feature);
            if (dbFeatures is not null)
            {
                entityShowcase.Features.Add(dbFeatures);
                continue;
            }
            
            entityShowcase.Features.Add(new Feature { Value = feature });
        }
        
        // after the loop above, enitityShowcase features is 2 (as it should be)

        var entityEntry = await _context.AddAsync(entityShowcase, cancellationToken);
        // after adding, it's actually 4...
        
        await _context.SaveChangesAsync(cancellationToken);
        return entityEntry.Entity.Id;
        var entityShowcase = new Models.Showcase
        {
            AuthorId = showcase.AuthorId,
            Description = showcase.Description,
            Title = showcase.Description,
            Summary = showcase.Summary,
            ImageSource = showcase.ImageSource,
            MajorVersion = showcase.MajorVersion,
            MinorVersion = showcase.MinorVersion,
            PatchVersion = showcase.PatchVersion,
        };

        foreach (var feature in showcase.Features)
        {
            var dbFeatures = _context.Feature.FirstOrDefault(x => x.Value == feature);
            if (dbFeatures is not null)
            {
                entityShowcase.Features.Add(dbFeatures);
                continue;
            }
            
            entityShowcase.Features.Add(new Feature { Value = feature });
        }
        
        // after the loop above, enitityShowcase features is 2 (as it should be)

        var entityEntry = await _context.AddAsync(entityShowcase, cancellationToken);
        // after adding, it's actually 4...
        
        await _context.SaveChangesAsync(cancellationToken);
        return entityEntry.Entity.Id;
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

❔ EF Core many-to-many migration problems
C#CC# / help
3y ago
EF Core many-to-many relation error
C#CC# / help
4y ago
EF Core mapping to Viewmodels without duplicating viewmodels
C#CC# / help
6mo ago
EF Core Many to Many relationship through Entity
C#CC# / help
14mo ago