-
-
Notifications
You must be signed in to change notification settings - Fork 159
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
EF many-to-many support #151
Comments
EF Core does not support many to many relationships without defining the joining entity. Ref dotnet/efcore#1368 Am I misunderstanding the question? |
You right, we have to use another entity to link main entities. Can you provide example for model decoration, f.e. Product - ProductTag - Tag. When I call GET /products/?include=tags I have to get Tags list, - how I can reach this? |
The json:api way of doing this would be to leverage deeply nested inclusions using relationships paths:
However, this is not currently supported. You may be able to implement a workaround at the repository layer (here and here). The other thing you would need to do is add a property on the model like so: class Product {
// ...
[NotMapped]
[HasMany("tags")]
public List<Tag> Tags { get; set; }
} Then in the repository, you should be able to do something like: public virtual async Task<Product> GetAndIncludeAsync(int id, string relationshipName)
{
if(relationshipName == "tags") {
var result = await Get()
.Include(p => p.ProductTags)
.ThenInclude(pt => pt.Tags)
.SingleOrDefaultAsync(e => e.Id.Equals(id));
result.Tags = result.ProductTags.Tags;
return result;
}
else {
return await base.GetAndIncludeAsync(id, relationshipName);
}
} For guidance on customizing repositories see the docs. The above is pseudo code that has not been compiled or tested, but is the approach I would take for now. Regarding direct support of many-to-many relationships, I do not plan on supporting that until EF Core supports it. However, I am open to working on deeply nested inclusions in the near future. My current focus is on #150 (Operations Support). Once that lands I'd be happy to move onto deeply nested inclusions. |
Thank you, now is clear. |
@LeonidEfremov please feel free to ping me on the project gitter channel if you need any support. |
@LeonidEfremov Thanks! |
@thomasirmer the above solution is how we're using it in other projects. However, after some thought I think it would be good to support this feature even after EF provides direct support for Many-To-Many w/o a join table. The API might look something like: public class Product : Identifiable
{
[NotMapped]
[HasMany("tags", through: nameof(ProductTags), by: nameof(ProductTag.Tag))]
public List<Tag> Tags { get; set; }
public virtual List<ProductTag> ProductTags { get; set; }
} |
Feat/#151: Many-to-Many Support via [HasManyThrough]
As I understand this is not supported? Can you add this ability?
The text was updated successfully, but these errors were encountered: