1

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 Answers2

1

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.

0

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();
  • 3
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jun 24 '22 at 09:16
  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/32088929) – Paulo Pereira Jun 28 '22 at 19:57