Answer the question
In order to leave comments, you need to log in
Linq, many-to-many relationship in EF?
There is a database implemented many-to-many relationship through the intermediate table ProductRepo.
After screwing the database into the project through EF, the database comes out without an intermediate table with a many-to-many relationship.
With such a query to the database:
Select Product.Name, Product.Price, Product.Count, Repository.Name
from Product, Repository, ProductRepo
where Product.Id = ProductRepo.IdProduct AND Repository.Id = ProductRepo.IdRepository
I get the result var query = db.Products.Include(x => x.Repositories).ToList();
var query = db.Products.Include("Repositories").ToList();
Select Product.Name, Product.Price, Product.Count, Repository.Name
from Product, Repository, ProductRepo
where Product.Id = ProductRepo.IdProduct AND Repository.Id = ProductRepo.IdRepository
Answer the question
In order to leave comments, you need to log in
In general, there are several options. We can manually add the desired linking table to the context.
Or do something like this:
var data = db.Products.Include("Repositories");
var values = new List<ProductWithRepoDTO>();
foreach(var record in data)
{
var reposArray = record.Repositories.Select(x=>x.Name).ToArray();
var reposString = string.Join(",", reposArray);
values.Add(new ProductWithRepoDTO
{
Id = record.Id,
Name = record.Name,
Cound = record.Count,
Price = record.Price,
Repository = reposString
});
}
//дальше работать с values
But it gives this error:
Connectusing System.Data.Entity;
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question