I'm building a Web API using .NET 6 and Entity Framework Core to connect to a SQL Server database. The basic stuff looks like it's working, but I cannot make the many-to-many relationships in my models work.
Considering the following 3 tables in the database:
Products:
Categories:
CategoryProduct:
I have created 3 models corresponding to these tables as follows:
public class Product
{
[Key]
public int Id { get; set; } = 0;
public string Name { get; set; } = string.Empty;
public string Description { get; set; } = string.Empty;
public string Label { get; set; } = string.Empty;
public int Sativa { get; set; } = 0;
public int Indica { get; set; } = 0;
public ICollection<CategoryProduct> Categories { get; set; } = new HashSet<CategoryProduct>();
}
public class Category
{
[Key]
public int Id { get; set; } = 0;
public string Name { get; set; } = string.Empty;
public int Order { get; set; } = 0;
public ICollection<CategoryProduct> Products { get; set; } = new HashSet<CategoryProduct>();
}
public class CategoryProduct
{
public int CategoryId { get; set; } = 0;
public Category? Category { get; set; } = null;
public int ProductId { get; set; } = 0;
public Product? Product { get; set; } = null;
}
I have created the following DbContext class to communicate with the database:
public class CoffeeshopContext : DbContext
{
public DbSet<Shop>? Shops { get; set; } = null;
public DbSet<Category>? Categories { get; set; } = null;
public DbSet<Product>? Products { get; set; } = null;
public DbSet<Price>? Prices { get; set; } = null;
public DbSet<CategoryProduct>? ProductCategories { get; set; } = null;
private readonly IConfiguration _configuration;
public CoffeeshopContext(DbContextOptions<CoffeeshopContext> options, IConfiguration configuration) : base(options)
{
_configuration = configuration;
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<CategoryProduct>().HasKey(x => new { x.CategoryId, x.ProductId });
modelBuilder.Entity<CategoryProduct>().HasOne(x => x.Product)
.WithMany(x => x.Categories)
.HasForeignKey(x => x.ProductId);
modelBuilder.Entity<CategoryProduct>().HasOne(x => x.Category)
.WithMany(x => x.Products)
.HasForeignKey(x => x.CategoryId);
base.OnModelCreating(modelBuilder);
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
optionsBuilder.UseSqlServer(_configuration.GetConnectionString(nameof(CoffeeshopContext)));
}
}
When I run the project using swagger, I get the following results when calling and endpoint which retrieves the Products:
[
{
"id": 3,
"name": "Ice Cream Bean",
"description": "Well balanced hybrid with a mood boosting euphoric high and body relaxation. Diesel and citrus aroma with creamy fruity undertones.",
"label": "CALI STRAIN",
"sativa": 50,
"indica": 50,
"categories": []
},
{
"id": 4,
"name": "Blue Cheese",
"description": "Deep relaxing, calming and pain relief. Berry, blue cheese aroma and a creamy taste.",
"label": "CLASSIC STRAIN",
"sativa": 20,
"indica": 80,
"categories": []
}
]
So, the endpoint is working and retrieving products from the database.
However, for some reason it does not make the relationship to categories (and yes, I am sure there are records in the ProductCategory table to define the relationship between products and categories). I am sure I am missing something, but I don't see what it is I'm missing. Can someone perhaps point me in the right direction on how to make this work?



