简体   繁体   English

实体框架:如何在一列中使用两个记录

[英]Entity Framework: how to use two records in one column

I'm creating a simply database with Entity Framework using ASP.NET, i have class "Graphics" and class "InputOutput". 我正在使用ASP.NET使用Entity Framework创建一个简单的数据库,我具有“图形”类和“ InputOutput”类。 I want to set in Initializer two records from InputOutput to Graphics one column. 我想在Initializer中设置从InputOutput到Graphics一列的两个记录。

Graphics.cs Graphics.cs

public class Graphics
    {
        public int GraphicsID { get; set; }
        public int ProducerID { get; set; }
        public int InputOutputID { get; set; }

        public string Name { get; set; }
        public string TypeOfConnection { get; set; }
        public string ProducerOfChipset { get; set; }
        public string TypOfMemory { get; set; }
        public string CoreSpeed { get; set; }
        public string MemoryClock { get; set; }
        public string MultiCardsTech { get; set; }
        public string HDCP { get; set; }
        public string CUDA { get; set; }
        public string VRReady { get; set; }
        public string TypeOfCooling { get; set; }

        public string Size { get; set; }

        public ICollection<InputOutput> InputOutput { get; set; }
        public ICollection<Producer> Producer { get; set; }
        public ICollection<LaptopProduct> LaptopProduct { get; set; }

    }

InputOutput.cs InputOutput.cs

public class InputOutput
    {
        public int InputOutputID { get; set; }
        public string Name { get; set; }

        public ICollection<Graphics> Graphics { get; set; }
    }

And from Initializer: 从初始化器:

var inputoutputs = new List<InputOutput>
            {
                new InputOutput { Name = "DVI"},
                new InputOutput { Name = "HDMI"},
                new InputOutput { Name = "VGA"}
            };

var graphics = new List<Graphics>
            {
                new Graphics { Name="GTX 1080", ProducerID=4, InputOutputID = "HERE ? What should i write to set 2 properties?" },
                new Graphics { Name="GTX 1070", ProducerID=4},
                new Graphics { Name="GTX 1080Ti", ProducerID=4},
            };

I want to display GTX1080 with eg two ports HDMI and VGA but i can only set one property to InputOutputID. 我想显示带有两个端口HDMI和VGA的GTX1080,但是我只能将一个属性设置为InputOutputID。

In your initializer you can reference your previous list inputoutputs . 在初始化程序中,您可以引用先前的列表inputoutputs

var graphics = new List<Graphics>
            {
                new Graphics { Name="GTX 1080", ProducerID=4, InputOutputID = inputoutput[0].InputOutputId, InputOutput = 
                   new List<InputOutput> { 
                           inputoutputs[0], inputoutputs[1] 
                   },
                new Graphics { Name="GTX 1070", ProducerID=4},
                new Graphics { Name="GTX 1080Ti", ProducerID=4},
            };

However, since this is a many to many, I'm not sure how the InputOutputID column is going to be used. 但是,由于这是多对多的,所以我不确定如何使用InputOutputID列。 You need the junction table to get the items you need. 您需要连接表来获取所需的项目。

Here's an example of many to many in EF. 这是EF中多对多的示例。 http://www.entityframeworktutorial.net/code-first/configure-many-to-many-relationship-in-code-first.aspx http://www.entityframeworktutorial.net/code-first/configure-many-to-many-relationship-in-code-first.aspx

Note, each table only have the collection to each other and none of the other table's ID attributes. 请注意,每个表仅具有彼此的集合,而另一个表的ID属性则没有。

I also subscribe to making my many to many tables explicit in my ORM mappings. 我也同意在ORM映射中使多对多表显式化。 AKA - I actually create the junction table in EF code. 又名-我实际上是用EF代码创建联结表。 Jimmy Bogard has a great example and reasons for this. 吉米·博加德(Jimmy Bogard)有一个很好的例子,并说明了原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM