'ASP.NET/.Net Core 6 - Foreign Key with different name

I'm contributing to a booking app : I need to add a feature where I can link a menu (eg. Sandwich or Salad) to an Order. I have two models :

    public partial class Product
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public int ProductTypeId { get; set; }
    }

Where ProductTypeId points to the type of item (1 = Meal, 2 = Dessert or 3 = Drink) (we don't want to have 3 separated tables for this)

And a Order_Product Model :

    public partial class Order_Product
    {
        public int OrderId { get; set; }
        public Order Order { get; set; }
        # Meal
        public Product MealProduct { get; set; }
        public int MealProductId { get; set; }
        public int MealProductTypeId { get; set; }
        # Dessert
        public Product DessertProduct { get; set; }
        public int DessertProductId { get; set; }
        public int DessertProductTypeId { get; set; }
        # Drink
        public Product DrinkProduct { get; set; }
        public int DrinkProductId { get; set; }
        public int DrinkProductTypeId { get; set; }
    }

So in that case, DessertProductId, MealProductId & DrinkProductId all match the same field Id in Product. EF is able to automatically get the Foreign Key logic for the Product.Id (I guess with the prefix) as seen below : Tables

... but not for the ProductTypeId field.

How can I declare (in FluentAPI) a Foreign Key that does not follow the generic naming ?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source