EF Core many to many relation on single column with composite primary key

16 Views Asked by At

I have following tables with composite primary keys (ID, Lang) and have many to many relation through CompanyCustomers table Tables and relations

            modelBuilder.Entity<Company>(entity =>
            {
                entity.ToTable("Companies").HasKey(pk => new { pk.ID, pk.Lang });

            });



            modelBuilder.Entity<Customer>(entity =>
            {
                entity.ToTable("Customers").HasKey(pk => new { pk.ID, pk.Lang });

            });



            modelBuilder.Entity<CompanyCustomer>(entity =>
            {
                entity.ToTable("CompanyCustomers").HasKey(pk => new { pk.CompanyID, pk.CustomerID });

            });
  1. How to configure many to many relation?
  2. How to write Linq (method syntax) to INNER join and return Customers of the company (following is method signature
public IEnumerable<Customer> GetCompanyCustomers(int companyId, string lang)
{
    // var result = _db.Companies.Join(...).Select(s=> s.Customer).ToList();
}
0

There are 0 best solutions below