Home > database >  Can't use my joining table from DbContext
Can't use my joining table from DbContext

Time:07-23

I have database with different tables. But I can't get access to some tables, and I'm curious why? For example, there is Projects table and Users table in database, and I need to get list of all users that are in specific Project by Id. These tables have many to many relationship, so there is joining table with ProjectId and UserId. It's logic to use some Join to get users that I need from table Users with help of this joining table from db. But how can I make Join when my DbContext doesn't have joining table? How can I get access to joining table using DbContext, or maybe there are some different ways to get list of users by project id? enter image description here

enter image description here

CodePudding user response:

ProjectUser - is joining table, use for link two tables. This tables must have (usually) Collection for access to this many-to-many data. So you can will have collection of users for this project. And doing filtering by users from your Ticket (example from image). Yes, exists few different ways - setting with ef fluent, or attribute, or dbcontext. Better to find complete guide for many-to-many setting in your application.

CodePudding user response:

If someone have similar problem, here is a simple way to use all the data from collections in model

var project = _db.Projects.Include(x => x.Tickets).ThenInclude(y => y.Users).SingleOrDefault(p => p.Id == id);

so after that you gonna have your model and all the data from collections that you need

  • Related