Search code examples
.net-coreentity-framework-coreef-core-2.2

EFCore Map 2 entities to same table - unable to add new entry


I have two separate aggregates, say Group and Brand, and a many-to-many relationship between these two. Since these are two separate aggregates, I have created separate bridging entities, BrandGroup and GroupBrand respectively. Now I want to map these two to the same table. Both entities are under the same DbContext. The following are my entity configurations:

public class BrandGroupEntityTypeConfiguration : BaseEntityTypeConfiguration<BrandGroup>
{
    public override void Configure(EntityTypeBuilder<BrandGroup> brandGroupConfiguration)
    {
        base.Configure(brandGroupConfiguration);
        brandGroupConfiguration.ToTable("BrandGroup");
        brandGroupConfiguration.Property(bc => bc.GroupId).IsRequired().HasColumnName(nameof(BrandGroup.GroupId));
        brandGroupConfiguration.Property(bc => bc.BrandId).IsRequired().HasColumnName(nameof(BrandGroup.BrandId));
        brandGroupConfiguration.HasIndex(bc => new { bc.GroupId, bc.BrandId }).IsUnique().HasFilter("[Deleted] = 0");
        brandGroupConfiguration.HasOne<GroupBrand>().WithOne().HasForeignKey<BrandGroup>(b => b.Id);
    }
}


public class GroupBrandEntityTypeConfiguration : BaseEntityTypeConfiguration<GroupBrand>
{
    public override void Configure(EntityTypeBuilder<GroupBrand> groupBrandConfiguration)
    {
        base.Configure(groupBrandConfiguration);
        groupBrandConfiguration.ToTable("BrandGroup");
        groupBrandConfiguration.Property(gb => gb.GroupId).IsRequired().HasColumnName(nameof(GroupBrand.GroupId));
        groupBrandConfiguration.Property(gb => gb.BrandId).IsRequired().HasColumnName(nameof(GroupBrand.BrandId));
    }
}

But, when I try to add new brand or group without corresponding brand-groups, it saves just fine. But if I try to add a brand-group relationship either through the Brands or Groups, it returns an error:

The entity of type 'GroupBrand' is sharing the table 'Configuration.BrandGroup' with entities of type 'BrandGroup', but there is no entity of this type with the same key value that has been marked as 'Added'.

I am not quite sure what I am missing.


Solution

  • Entity Framework is complaining that the state is invalid.

    Basically, it sees that a BrandGroup has been added, but there isn't the same entity in the GroupBrand. And yet they are the same table.

    So according to Entity Framework, has something been added or not? If it looks at BrandGroup, it might conclude that something has been added, but if it looks at GroupBrand, nothing has been added. So there is a conflict.

    Normally in a many-to-many relationship, you would have something like

           ╔════════════╗
    Group ═╣ GroupBrand ╠═ Brand
           ╚════════════╝
    

    So both a Brand object and a Group object would have a collection of GroupBrand objects.

    So rather than having GroupBrand and BrandGroup, I'd have just one or the other.