3

I would like to make app for restaurant. It´s about calculation recipes.

I have domain classes: - Ingredient - Recipe - RecipeItem

Recipe has List of RecipeItem.

RecipeItem could be Ingredient but also Recipe.

So I'm using Interface IItem with 2 properties(Id, Name). If I use Interface in my class, db generator ignore this field.

Look at my classes here:

public class Ingredient : IItem
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public double Price { get; set; }
    }

public class Recipe : IItem
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public List<RecipeItem> RecipeItems { get; set; }
    }

public class RecipeItem
    {
        public int Id { get; set; }
        public IItem Item { get; set; }
        public double Quantity { get; set; }
    }

CodeFirst automaticly generate for me database with tables above.

I would like to have database table RecipeItem looks likes:

> Id 
> Quantity 
> Recipe_Id - FK of Recipe ID 
> RecipePointer - Nullable FK of specific Recipe
> IngredientPointer - Nullable FK of specific Ingredient

But there are only:

> Id 
> Quantity 
> Recipe_Id - FK of Recipe ID 

If I put into RecipeItem for example Ingredient, than Id of Ingredient will be inserted to IngredientPointer.

I would like to use FLUENT API for mapping but I don't know how.

I don't know if is possible what I want, or if is any better way how to solve my problem.

I watched already CodeFirst video on Pluralsight and I browse in forums but I can't find answer.

Thank you for any help.

2 Answers 2

3

Not sure it can be done, but if it can, do not use an interface but a class, please try

public class Item
{
    public int Id { get; set; }
    public string Name { get; set; }
}

public class Ingredient : Item
{
    public double Price { get; set; }
}

public class Recipe : Item
{
    public List<RecipeItem> RecipeItems { get; set; }
}

public class RecipeItem
{
    public int Id { get; set; }
    public Item Item { get; set; }
    public double Quantity { get; set; }
}

Then you may want to read about EF and TPH

Sign up to request clarification or add additional context in comments.

Comments

0

you need to use the DataAnnotations to clarify it for EF how to generate the DB Generation Script

for your solution i can propose you

use the InversProperty and ForeignKey Attributes

have a look at the following resources :

https://msdn.microsoft.com/en-us/data/jj193542.aspx

https://msdn.microsoft.com/en-us/data/gg193958.aspx

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.