-1

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 ?

Ben
  • 24
  • 1
  • 3
  • https://stackoverflow.com/questions/20886049/ef-code-first-foreign-key-without-navigation-property – Code Name Jack May 09 '22 at 09:16
  • 1
    Does this answer your question? [EF Code First foreign key without navigation property](https://stackoverflow.com/questions/20886049/ef-code-first-foreign-key-without-navigation-property) – Code Name Jack May 09 '22 at 09:16

0 Answers0