-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
How do I define this relationship? #17642
Comments
This is currently not possible, it requires support for Polymorphic Relationships and transparent Many-to-Many |
Why has this been closed with references to unrelated functionality ???
Can you explain the relationship between my problem and the functionality you're referencing? |
@TehWardy Polymorphic relationships would allow to collapse two navigations to the same type into one: public class ParentChild
{
public string ParentId { get; set; }
public string ChildId { get; set; }
public virtual Thing Parent { get; set; }
public virtual Thing Child { get; set; }
}
public class Thing
{
public int Id { get; set; }
public virtual ICollection<ParentChild> RelatedThings { get; set; }
} The reason I mentioned transparent Many-to-Many is because you reference public virtual ICollection<Thing> RelatedThings { get; set; } |
Oh ... EF needs polymorphism to do the collapsing ? I thought the many to many thing was specificaly about hiding the join table which I don't mind having ... This looks good enough if EF could work with it somehow ...
... Just wondering if there is some sort of work around to make the object model work but with correct DB structure even it requires a bit of jankyness in the code. |
@TehWardy You could make the navigations private and expose a read-only one that combines them public class Thing
{
public int Id { get; set; }
private ICollection<ParentChild> Parents { get; set; }
private ICollection<ParentChild> Children { get; set; }
public virtual IEnumerable<ParentChild> RelatedThings => Parents.Concat(Children);
} Either way your current model and all the variants above will result in the same db structure, so it would be easy to change once the required features are supported. |
sounds good ... do you know if this will work with the OData framework when expanding in to the RelatedThings collection and performing queries on it? |
@TehWardy You wouldn't be able to use RelatedThings for server-side queries. |
I found a sort of "hack" that i'm testing right now where I expose one of the collections and tell the OData framework to ignore the other like this ...
I'm having to write some code on the back end so that when a RelatedThing with From = "A" and To = "B" is added or removed it also adds the opposite From = "B" and To = "A" ... it's a bit of a hack but it gives the desired functionality and only exposes 1 collection to the API layer keeping the "oddities" in my back end code. It's not ideal but does mean that I get full CRUD on it through EF with the extra management logic wrapped around add's / removed with the join table. Thanks for the help on this @AndriySvyryd ... hoping to see support for this properly in 3.0 |
ok further testing revaled that this won't work as it causes this ... |
I've hit a weird scenario in a many to many join.
typically you have a parent and a child like so ...
... this is a "web of data" i'm looking to define a situation where ParentChild has just one collection of "Related" where neither is the parent or the child they are simply related like this ...
... is there a way to achieve this in EF Core as I can't seem to figure out how to tell EF what i mean here?
The text was updated successfully, but these errors were encountered: