2013-07-24 210 views
3

我想为餐厅制作应用程序。这是关于计算配方。实体框架代码优先 - 界面

我有域类: - 成分 - 配方 - RecipeItem

配方有RecipeItem的名单。

RecipeItem可以成分,但也可以食谱。

所以我使用Interface IItem和2个属性(Id,Name)。 如果我在我的类中使用接口,db生成器忽略此字段。

在这里看看我的课:

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全自动生成数据库我上面的表。

我想有数据库表RecipeItem看起来喜欢:

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

但只有有:

> Id 
> Quantity 
> Recipe_Id - FK of Recipe ID 

如果我投入RecipeItem例如成分,比成分的ID将被插入成分指示剂。

我想使用FLUENT API进行映射,但我不知道如何。

我不知道是否有可能我想要什么,或者如果有什么更好的办法如何解决我的问题。

我看过Pluralsight上的CodeFirst视频,我在论坛中浏览,但找不到答案。

谢谢你的帮助。

回答

3

不知道这是可以做到的,但如果它可以,不要用一个接口,但一个班,请尝试

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; } 
} 

那么你可能要阅读有关EF and TPH

+0

谢谢你,它可以帮助我。 – tonco