Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Define an Unidirectional Many to Many without Join Table #34775

Closed
samusaran opened this issue Sep 27, 2024 · 1 comment
Closed

Define an Unidirectional Many to Many without Join Table #34775

samusaran opened this issue Sep 27, 2024 · 1 comment

Comments

@samusaran
Copy link

Hi, I found myself in a strange situation that I don't know how to setup in EF Core.

I have the following 2 entities:

// Primary key is CountryCode+ServiceProdId+ModelId
public sealed class LTRServiceProduct
{
    [StringLength(2)]
    public string CountryCode { get; init; } = null!;
    
    public int ServiceProdId { get; init; }

    public int ModelId { get; init; }

    public IEnumerable<LTRServiceProductFactor> Factors { get; init; } = new List<LTRServiceProductFactor>();
   ....
}

[PrimaryKey(nameof(ServiceProdId), nameof(FactorId))]
public sealed class LTRServiceProductFactor
{
    public int ServiceProdId { get; init; }
    
    public int FactorId { get; init; }
}

I want to create a relationship (even without foreign keys, which may be a case related to #15854) where i relate those 2 entities just using ServiceProdId.

In my case, this two entities are readonly on database, so the application will never write to those tables, and the relationship will always start from LTRServiceProduct to LTRServiceProductFactor (so i'll never try to get all the Products related to a ProductFactor because it has no sense).

How should I configure this navigation? Building a Many-to-Many is wrong because basically the bridge table is bigger than the destination table, and also requires me to populate that table with a crossjoin of data that shouldn't be needed and would be redundant.

But a One-to-Many fails because it enforces a unique constraint during migration on ServiceProdId when it's not unique.

Any help is appreciated.

Thank you
Alex

@AndriySvyryd
Copy link
Member

Duplicate of #20337

@AndriySvyryd AndriySvyryd marked this as a duplicate of #20337 Oct 3, 2024
@AndriySvyryd AndriySvyryd closed this as not planned Won't fix, can't repro, duplicate, stale Oct 3, 2024
@AndriySvyryd AndriySvyryd removed their assignment Oct 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants