EF Core 6.0 One-To-Many one-way link with middle table using another table primary key

33 views Asked by At

I am having trouble trying to link an object to a list of Addresses using a middle table that is uses a different table's primary key with the primary key for Address. I want a one-to-many one-way link from the object to Address.

So I created the follow:

public class GlobalEntity
{
    [Key]
    public long Id { get; set; }
}

public class EntityAddress
{
    [Required]
    public int GlobalEntityId { get; set; }
    public GlobalEntity GlobalEntity { get; set; } = null!;
    [Required]
    public int AddressId { get; set; }
    public Address Address { get; set; } = null!;
}


public class Location
{
    [Key]
    public int Id { get; set; }
    public string Name { get; set; } = null!;

    [Required]
    public int GlobalEntityId { get; set; }
    public GlobalEntity GlobalEntity { get; set; } = null!;

    public List<Address> Addresses { get; set; } = null!;

}
public class Manufacturer
{
    [Key]
    public int Id { get; set; }
    public string Name { get; set; } = null!;

    [Required]
    public int GlobalEntityId { get; set; }
    public GlobalEntity GlobalEntity { get; set; } = null!;

    public List<Address> Addresses { get; set; } = null!;

}

public class Address
{
    [Key]
    public int Id { get; set; }

    public string Label { get; set; } = null!;

    public string Country { get; set; } = null!;

    public string State { get; set; } = null!;

    public string City { get; set; } = null!;

    public string Zip { get; set; } = null!;
    public string StreetAddress { get; set; } = null!;

    [Required]
    public int GlobalEntityId { get; set; }
    public GlobalEntity GlobalEntity { get; set; } = null!;
}

This program can expand bigger and bigger with other objects in the future that may have List. What I'm trying to achieve at this time is using one middle table to link both Manufacturer and Location to a List without having to create a middle table for each one. There is already a GlobalEntity table which generates a unique id for every single object created such as Location, Manufacturer, and Address.

How can I go about using this middle table to link Location to a list of Address, preferably without adding navigation properties to Address? I am hoping to use fluent API to achieve this.

I tried the following to link but I keep getting errors.

protected override void OnModelCreating(ModelBuilder builder)
    {
        base.OnModelCreating(builder);


        builder.Entity<EntityAddress>(ea =>
        {
            //Configures one-to-many relationship between Objects and Address through EntityAddress
            ea.HasKey(e => new { e.GlobalEntityId, e.AddressId });

            ea.HasOne(e => e.GlobalEntity)
                .WithMany()
                .HasForeignKey(e => e.GlobalEntityId);

            ea.HasOne(e => e.Address)
                .WithMany()
                .HasForeignKey(e => e.AddressId);
        });

        builder.Entity<DigitalMarket>(dm =>
        {
            dm.HasMany(d => d.Addresses)
                .WithMany()
                .UsingEntity<EntityAddress>(
                    j => j.HasOne(ea => ea.Address).WithMany().HasForeignKey(ea => ea.AddressId),
                    j => j.HasOne(ea => ea.GlobalEntity).WithMany().HasForeignKey(ea => ea.GlobalEntityId)
                );

        });
    }

The first WithMany() in the Entity throws an error saying that it needs arguments, which would require a navigation property from Address. But when I change it to WithOne() I can't find a way to configure it so that I actually Links List Addresses through the middle table.

Any help figuring this out would be greatly appreciated.

0

There are 0 answers