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

a join entity instead of a shadow foreign key #3606

Closed
yecril71pl opened this issue Dec 10, 2021 · 8 comments
Closed

a join entity instead of a shadow foreign key #3606

yecril71pl opened this issue Dec 10, 2021 · 8 comments

Comments

@yecril71pl
Copy link
Contributor

yecril71pl commented Dec 10, 2021

I have a situation when entity A has many entities C and entity B has many entities C too, entities A and B being different and unrelated. If I understand the description correctly, the framework will inject optional shadow foreign key for entities A and B into the property C. I would prefer to have separate join entities AhasC and BhasC in the model.

I understand that I can say "C has many A with many via A.Cs" but that reads counter-intuitive (a location has many inhabitants reads all right, but inhabitants is an abstract word coined specially for this case, so as to avoid the cringe of saying a location has many persons; the construct is by no means generic). I would expect to be able to say "A has many C with many" but that does not work because the sentence is incomplete without a collection property. But C does not have a collection property for A, nor should it have.


Document Details

Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

@ajcvickers
Copy link
Contributor

@yecril71pl This tracked by dotnet/efcore#3864. The workaround for now is to create a private collection navigation, or even make it a field.

@yecril71pl
Copy link
Contributor Author

yecril71pl commented Dec 11, 2021

@yecril71pl This tracked by dotnet/efcore#3864. The workaround for now is to create a private collection navigation, or even make it a field.

My pain point is not visibility but the fact that I would have to add a navigation field in C for every superior that has C as a dependent. The only acceptable workaround is using an explicit association class. However, the natural candidate, a KeyValuePair, is not accepted because it is a value type (see #3604) and cannot be normally navigated because it is keyless, which is normal for an association record. It can be navigated as an owned entity but this is kinda hard to figure out because that is advanced stuff (at least in the exposition order). This problem is so severe it should at least be mentioned in the handout.

@ajcvickers
Copy link
Contributor

@yecril71pl Can you share an example of C# code you want to use to define the entity types in this case?

@yecril71pl
Copy link
Contributor Author

yecril71pl commented Dec 14, 2021

class Person {
    public int Id { get; set; }
    public IEnumerable < PersonAddress > Addresses { get; set; }}

class Address {
    public int Id { get; set; }}

class PersonAddress {
    public Person Person { get; set; }
    public Address Address { get; set; }
}

@ajcvickers
Copy link
Contributor

@yecril71pl Supporting this shape is exactly what is covered by dotnet/efcore#3864

@yecril71pl
Copy link
Contributor Author

I understand, I just do not approve of your workaround. I believe I have a better one.

@ajcvickers
Copy link
Contributor

@yecril71pl Feel free to comment on that issue with your better workaround. People can choose to use it if they wish.

@yecril71pl
Copy link
Contributor Author

Is #3606 (comment)
I hoped to get some feedback about how fragile/inappropriate/whatever it is. But I shall go with it FTTB and see how it goes.

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