我正在尝试创建一个快速的演示商店,但我失败了optional many to one or zero
关系。
相关课程是:
项目
public class Item
{
public int ID { get; set; }
public string Name { get; set; }
public int SubCategoryID { get; set; }
public virtual SubCategory Category { get; set; }
public double Price { get; set; }
}
顺序
public class Order
{
public int ID { get; set; }
public DateTime DateOfOrder { get; set; }
public virtual ICollection<Item> Items { get; set; }
public int CustomerID { get; set; }
public virtual Customer Customer { get; set; }
}
然而,我感到困惑,因为查看模型显示:
但是,数据库本身显示(对于项目):
这表明每件商品只能属于一个订单。
我是否必须创建一个包含多个订单/项目的单独类?
我似乎记得EF自动执行此操作,但是,我几年没有触及它,我只能记住以前的事情。
答案 0 :(得分:0)
我不得不补充道:
00:00.0 Host bridge [0600]: Intel Corporation Core Processor DRAM Controller [8086:0044] (rev 12)
Subsystem: Dell Device [1028:0434]
Kernel driver in use: agpgart-intel
00:02.0 VGA compatible controller [0300]: Intel Corporation Core Processor Integrated Graphics Controller [8086:0046] (rev 12)
Subsystem: Dell Device [1028:0434]
Kernel driver in use: i915
00:16.0 Communication controller [0780]: Intel Corporation 5 Series/3400 Series Chipset HECI Controller [8086:3b64] (rev 06)
Subsystem: Dell Device [1028:0434]
Kernel driver in use: mei_me
00:1a.0 USB controller [0c03]: Intel Corporation 5 Series/3400 Series Chipset USB2 Enhanced Host Controller [8086:3b3c] (rev 06)
Subsystem: Dell Device [1028:0434]
Kernel driver in use: ehci-pci
00:1b.0 Audio device [0403]: Intel Corporation 5 Series/3400 Series Chipset High Definition Audio [8086:3b56] (rev 06)
Subsystem: Dell Device [1028:0434]
Kernel driver in use: snd_hda_intel
00:1c.0 PCI bridge [0604]: Intel Corporation 5 Series/3400 Series Chipset PCI Express Root Port 1 [8086:3b42] (rev 06)
Kernel driver in use: pcieport
00:1c.5 PCI bridge [0604]: Intel Corporation 5 Series/3400 Series Chipset PCI Express Root Port 6 [8086:3b4c] (rev 06)
Kernel driver in use: pcieport
00:1d.0 USB controller [0c03]: Intel Corporation 5 Series/3400 Series Chipset USB2 Enhanced Host Controller [8086:3b34] (rev 06)
Subsystem: Dell Device [1028:0434]
Kernel driver in use: ehci-pci
00:1e.0 PCI bridge [0604]: Intel Corporation 82801 Mobile PCI Bridge [8086:2448] (rev a6)
00:1f.0 ISA bridge [0601]: Intel Corporation Mobile 5 Series Chipset LPC Interface Controller [8086:3b09] (rev 06)
Subsystem: Dell Device [1028:0434]
Kernel driver in use: lpc_ich
00:1f.2 SATA controller [0106]: Intel Corporation 5 Series/3400 Series Chipset 4 port SATA AHCI Controller [8086:3b29] (rev 06)
Subsystem: Dell Device [1028:0434]
Kernel driver in use: ahci
00:1f.3 SMBus [0c05]: Intel Corporation 5 Series/3400 Series Chipset SMBus Controller [8086:3b30] (rev 06)
Subsystem: Dell Device [1028:0434]
00:1f.6 Signal processing controller [1180]: Intel Corporation 5 Series/3400 Series Chipset Thermal Subsystem [8086:3b32] (rev 06)
Subsystem: Dell Device [1028:0434]
Kernel driver in use: intel ips
03:00.0 Ethernet controller [0200]: Realtek Semiconductor Co., Ltd. RTL8101E/RTL8102E PCI Express Fast Ethernet controller [10ec:8136] (rev 02)
Subsystem: Dell Device [1028:0434]
Kernel driver in use: r8169
ff:00.0 Host bridge [0600]: Intel Corporation Core Processor QuickPath Architecture Generic Non-core Registers [8086:2c62] (rev 02)
Subsystem: Intel Corporation Device [8086:8086]
ff:00.1 Host bridge [0600]: Intel Corporation Core Processor QuickPath Architecture System Address Decoder [8086:2d01] (rev 02)
Subsystem: Intel Corporation Device [8086:8086]
ff:02.0 Host bridge [0600]: Intel Corporation Core Processor QPI Link 0 [8086:2d10] (rev 02)
Subsystem: Intel Corporation Device [8086:8086]
ff:02.1 Host bridge [0600]: Intel Corporation Core Processor QPI Physical 0 [8086:2d11] (rev 02)
Subsystem: Intel Corporation Device [8086:8086]
ff:02.2 Host bridge [0600]: Intel Corporation Core Processor Reserved [8086:2d12] (rev 02)
Subsystem: Intel Corporation Device [8086:8086]
ff:02.3 Host bridge [0600]: Intel Corporation Core Processor Reserved [8086:2d13] (rev 02)
Subsystem: Intel Corporation Device [8086:8086]
到项目......我永远不会这样称呼它,但看起来这是EF建立这种关系所必需的。
我确信它过去更容易,所以,请将这个问题保持开放,以便有人能给出更好的答案!
答案 1 :(得分:0)
如果您向Orders
实体添加Item
的集合,EF将隐式为您创建数据库上的junction table。在此page中,您可以找到有关如何配置多对多关系的更多信息。
当您需要添加其他列(排除要加入的表的两个键)时,通常会映射联结表。在这种情况下,您需要在表示联结表的实体与Order
和Item
之间创建两个一对多关系。有些人建议始终映射联结表,因为这样您将所有表表示为实体,并且您可以从联结表开始编写查询。您可以在link中找到有关此主题的有趣信息。但是,根据我的经验,在大多数情况下,您可以完美地工作,而无需明确地映射联结表。