ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

c#-实体框架代码优先-接口

2019-11-23 01:04:49  阅读:309  来源: 互联网

标签:ef-code-first interface code-first c entity-framework


我想为餐厅制作应用程序.关于计算配方.

我有域类:
-成分
– 食谱
-RecipeItem

配方具有RecipeItem列表.

RecipeItem可以是成分,也可以是配方.

所以我正在使用具有2个属性(Id,名称)的Interface IItem.
如果我在课堂上使用Interface,则db generator会忽略此字段.

在这里查看我的课程:

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将插入到IngredientPointer.

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

我不知道是否可能我想要什么,或者是否有更好的方法来解决我的问题.

我已经在Pluralsight上观看了CodeFirst视频,并在论坛中浏览,但找不到答案.

感谢您的任何帮助.

解决方法:

不确定是否可以完成操作,但是如果可以,请不要使用接口,而是使用类,请尝试

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

标签:ef-code-first,interface,code-first,c,entity-framework
来源: https://codeday.me/bug/20191123/2064154.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有