I'm using Entity Framework Core and Fluent API.
I'm trying to implement a relatedProducts array that is included in my product model like so:
{
"id": 13
"name": "something",
"heading": "something else",
"summary": "please put me out my misery!"
"relatedProducts": [
{
"name": "something related",
"heading": "something else related",
"summary": "something in summary"
},
{
"name": "something related",
"heading": "something else related",
"summary": "something in summary"
}
]
}
I currently have this in my Context:
modelBuilder.Entity<RelatedProduct>()
.HasKey(r => new { r.ProductId, r.RelatedId });
modelBuilder.Entity<RelatedProduct>()
.HasOne(p => p.Product)
.WithMany(r => r.RelatedProducts)
.HasForeignKey(p => p.ProductId)
.OnDelete(DeleteBehavior.Restrict);
modelBuilder.Entity<RelatedProduct>()
.HasOne(p => p.Related)
.WithMany(r => r.ProductsRelated)
.HasForeignKey(p => p.RelatedId)
.OnDelete(DeleteBehavior.Restrict);
My Related Product class looks like this:
public class RelatedProduct
{
public int ProductId { get; set; }
public virtual Product Product { get; set; }
public int RelatedId { get; set; }
public virtual Product Related { get; set; }
}
The trouble is my relatedProduct array is just coming back empty even though I've linked up the Ids in the db.
Any assistance on this would be awesome as i don't want to end up 'Bodging' it!