Many-to-Many Entity Framework Core 6 (junction table not generated)

1.6k views Asked by At

I've just started to work with Entity Framework Core 6. I am working with a sample database where I have a many to many relationship.

I created my database on SQL server. I created three tables: Service, Document, ServiceDocs (used as a Junction Table).

Then I did :

scaffolf-dbcontext

both classes have been generated except the junction table ServiceDocs. My question is: How can I add elements to the junction table and get data from it without the class of the junction table?

Thank you for your help.

Class document: 

 public partial class Document
    {
        public Document()
        {
            Services = new HashSet<Service>();
        }

        public Guid DocumentId { get; set; }
        public string? DocTitre { get; set; }

        public virtual ICollection<Service> Services { get; set; }
    }



 public partial class Service
    {
        public Service()
        {
            Docs = new HashSet<Document>();
        }

        public Guid ServiceId { get; set; }
        public string? Libelle { get; set; }

        public virtual ICollection<Document> Docs { get; set; }
    }

Here some screenshots : Database diagram Document

Service

2

There are 2 answers

2
Ravindra Maurya On BEST ANSWER

var result = await _dbContext.BillingGroupFakes
.Where(b => b.Customers.FirstOrDefault().ExternalCustomerId.Equals($"{id}"))
.Include(b => b.Customers)
.Select(m => new
{
m.Customers.FirstOrDefault().CustomerId,
CustomerName = $"{m.Customers.FirstOrDefault().CustomerLastName}, {m.Customers.FirstOrDefault().CustomerName}",
m.BillingGroupId,
m.BillingGroupCode,
m.BillingGroupDescription
})
.AsNoTracking()
.ToListAsync();

0
Element scan On

I found the answer how to get the data:

var services = await _context.Services
  .Where(s => s.ServiceId == Id)
  .Include(s =>s.Docs)
  .ToListAsync();

return services;

Thank you.