public class ProductOrder
{
public int PId { get; set; }
public Product Product { get; set; }
public int OId { get; set; }
public Order Order { get; set; }
}
public class ProductOrderDto
{
public IEnumerable<Product> Products { get; set; }
public IEnumerable<Order> Orders { get; set; }
}
public class EfProductOrderDal : EfEntityRepositoryBase<ProductOrderDto, EtnContext>, IProductOrderDal
{
public List<ProductOrderDto> GetProductOrderDetails()
{
using (EtnContext context = new EtnContext())
{
var model = new ProductOrderDto();
model.Products = context.Products.Include(i => i.ProductOrders).ThenInclude(i => i.Order).ToList();
model.Orders = context.Orders.Include(i => i.ProductOrders).ThenInclude(i => i.Product).ToList();
return model;
}
}
}
i would like to perform data insertion and data display of the tables that i relate to many to many, but i am receiving such a mistake
I would like to perform data insertion and data display of the tables that i relate to many to many, but i am receiving such a mistake.
Well, based on your scenario you might want to either return ProductOrderDto which would containing List of Products and Orders or you would like to return List of ProductOrderDto with collection of Products and Orders. So here are the both way how you could implement those:
Return Collection Of ProductOrderDto:
public List<ProductOrderDto> GetProductOrderDetails()
{
using (EtnContext context = new EtnContext())
{
//As your method signature is List<ProductOrderDto> thus, define your list type
var model = new List<ProductOrderDto>();
//Get each collection of products and Orders
var products = context.Products.Include(i => i.ProductOrders).ThenInclude(i => i.Order).ToList();
var orders = context.Orders.Include(i => i.ProductOrders).ThenInclude(i => i.Product).ToList();
//Bind products and Orders Into List
model.Add(products);
model.Add(orders);
// Now You can return your list
return model;
}
}
Explanation: As you have defined your method return type List so you eventually require to return of type list as well. But you have defined single instance as new ProductOrderDto(); Thus, your return type doesn't match. Therefore, if you would like to assign list of Products and Orders Collection, you ought to create new List() and then finally, you can assign collection into it by model.Add. . You can get more details in our official document here.
Alternative Way:
public List<ProductOrderDto> GetProductOrderDetails()
{
using (EtnContext context = new EtnContext())
{
var model = new List<ProductOrderDto>();
model.Add(context.Products.Include(i => i.ProductOrders).ThenInclude(i => i.Order).ToList());
model.Add(context.Orders.Include(i => i.ProductOrders).ThenInclude(i => i.Product).ToList());
return model;
}
}
Return Single ProductOrderDto:
public ProductOrderDto GetProductOrderDetails()
{
using (EtnContext context = new EtnContext())
{
var model = new ProductOrderDto();
model.Products = context.Products.Include(i => i.ProductOrders).ThenInclude(i => i.Order).ToList();
model.Orders = context.Orders.Include(i => i.ProductOrders).ThenInclude(i => i.Product).ToList();
return model;
}
}
Note: As your ProductOrderDto has the type of IEnumerable and IEnumerable Orders so when you would return ProductOrderDto itself it would be containing the both product and Order collection so that you can directly assign list to your ProductOrderDto object which is Products and Orders.
If you would like to invest few more time on above concept you could have a look on official document here.