C# 具有多对多关系的代码优先模型POCO

C# 具有多对多关系的代码优先模型POCO,c#,ef-code-first,asp.net-web-api,C#,Ef Code First,Asp.net Web Api,这是我一直在做的一个小练习,以习惯使用“代码优先”方法进行编码。 事实上,如果我“保守地”“先建立数据库”,我会设置得更快,我想,但那样的话,我就不会尝到这种新东西的味道了,所以我想尝试一下。 以下是我在应用程序中需要的实体: 项目(几乎只有一个标题、一个描述和一个id,后面可能还有其他信息) ItemPrice(如标题所示,它应表示某个项目在某个时间的价格,因此它需要能够引用某个项目,并存储日期时间值和价格的小数点) ItemRecipe(它包含一种创建项目的方法,可能有不同的方法来构造项目

这是我一直在做的一个小练习,以习惯使用“代码优先”方法进行编码。 事实上,如果我“保守地”“先建立数据库”,我会设置得更快,我想,但那样的话,我就不会尝到这种新东西的味道了,所以我想尝试一下。 以下是我在应用程序中需要的实体:

  • 项目(几乎只有一个标题、一个描述和一个id,后面可能还有其他信息)
  • ItemPrice(如标题所示,它应表示某个项目在某个时间的价格,因此它需要能够引用某个项目,并存储日期时间值和价格的小数点)
  • ItemRecipe(它包含一种创建项目的方法,可能有不同的方法来构造项目,这就是为什么它不是项目类本身的一部分,所以它引用一个目标项目和一个元组列表[项目/金额对])
现在,对于插入和更新项目和价格,这对我来说很有效,我的问题是菜谱;我还不能理解如何编写配方POCO(我认为包含列表的
virtual
甚至可能是错误的)

我写了POCO课程:

项目:

项目价格:

    [DataContract]
    public class ItemPrice
    {
        public int ID { get; set; }

        [Required(ErrorMessage = "You need to select an item for this price.")]
        [DataMember(IsRequired = true)]
        public int ItemID { get; set; }

        [ForeignKey("ItemID")]
        public virtual Item Item { get; set; }

        [Required(ErrorMessage = "You need to set a price for this item.")]
        [DataMember(IsRequired = true)]
        public Decimal Price { get; set; }

        [Required(ErrorMessage = "You need to provide a date and time for the price for this item.")]
        [DataMember(IsRequired = true)]
        public DateTime Time { get; set; }
    }
项目配方:

    [DataContract]
    public class ItemRecipe
    {
        public int ID { get; set; }

        [Required(ErrorMessage = "You need to select an item for this recipe.")]
        [DataMember(IsRequired = true)]
        public int ItemID { get; set; }

        [ForeignKey("ItemID")]
        public virtual Item TargetItem { get; set; }

        public virtual List<Tuple<Item, int>> Ingredients { get; set; }
    }
[DataContract]
公共类ItemRecipe
{
公共int ID{get;set;}
[必需(ErrorMessage=“您需要为此配方选择一个项目。”)]
[数据成员(IsRequired=true)]
公共int ItemID{get;set;}
[外键(“项目ID”)]
公共虚拟项TargetItem{get;set;}
公共虚拟列表{get;set;}
}
有人能告诉我怎么设置吗? 另外,我如何告诉这个元组列表,它的项部分实际上引用了一个不能在列表中出现两次的现有项?这甚至可以仅通过数据注释来实现吗?如果不是,我需要什么


基本上,我只想像本项目中的其他实体一样访问配方,我使用通用存储库来检索/操作值。我使用的api控制器只有简单的Get-/Post-/Put-/Delete方法,因此为了更新或创建配方,我只需覆盖它(而不是逐个插入配料项目量)。

列表不起作用-并且代码中没有流畅的配置。简言之,我认为您需要
ItemItemRecipe
索引表,多对多-它具有复合项,ItemRecipe ID-s+标记以及“int”作为部分金额。为此,您需要在代码中完成,而无法通过注释完成。你可以查看我之前关于如何做的帖子啊,我已经担心了,不过还是要感谢你的输入:)你能再详细介绍一下什么是索引表以及我如何设置它吗?我想这就是你得到的:)-如果没有更好的结果,如果这有助于让我知道发布答案,或者让我知道fluent是否有问题,我会解释更多。有一件事需要澄清,如果我现在使用fluent api,我将根本无法使用数据注释,对吗?因为我重写了DBContext的OnModelCreating?不,它们协同工作-您可以组合,但不建议组合做相同事情的功能。您可以将“Fluent配置”视为一种附加配置。事实上,我只会在你习惯的时候使用fluent,因为我更喜欢把所有的配置都放在一个地方,但这样更好,并不是说你做不到。
    [DataContract]
    public class ItemRecipe
    {
        public int ID { get; set; }

        [Required(ErrorMessage = "You need to select an item for this recipe.")]
        [DataMember(IsRequired = true)]
        public int ItemID { get; set; }

        [ForeignKey("ItemID")]
        public virtual Item TargetItem { get; set; }

        public virtual List<Tuple<Item, int>> Ingredients { get; set; }
    }