locked
Get related products RRS feed

  • Question

  • User1968449888 posted

    Hi

    I am developing a section to return some related products.

    public class Product : EntityBase
    {
         private List<Product> _relatedProducts;
         //some parameters
         public virtual ICollection<Product> RelatedProducts => _relatedProducts;
    }
    
    public class CompatibleProducts
    {
                public virtual Product LeftProduct { get; }
                public virtual Product RightProduct { get; }           
    }
    builder.HasMany(x => x.RelatedProducts)
                    .WithMany(x => x.RelatedProducts)
                    .UsingEntity<CompatibleProducts>(
                        b =>
                            b.HasOne(x => x.RightProduct)
                                .WithMany(),
                               
                        b =>
                            b.HasOne(x => x.LeftProduct)
                                .WithMany(),
                                   
                        b =>
                        {
                            b.ToTable("CompatibleProducts");
                            b.HasKey( new[] {"RightProductId", "LeftProductId"});
                        }
                    );

    When I try to get products and it's related products, the result of CompatibleProducts is null but I expect to get CompatibleProduct 23

    var product = await _db.Products.FindAsync(request.ProductId);//ProductId=12

    CompatibleProducts table:

    LeftProductId        RightProductId
    -----------------   --------------------
         12                   23

    Tuesday, March 16, 2021 12:21 PM

All replies

  • User1312693872 posted

    Hi,Ocelot013

    I can't reproduce your problem, could you please show more code?  How did you use many-to-many on the same table and what is the

    request?

    Best Regards,

    Jerry Cai

    Wednesday, March 17, 2021 8:40 AM