I'm designing a database to keep inventory of office equipment, but in many cases a room will contain multiples of an identical item, like a conference room with 3 identical TV screens. Storing serial numbers and such isn't important; only quantity matters.
我正在设计一个数据库来保存办公设备的库存,但在很多情况下,一个房间将包含多个相同的项目,就像一个有3个相同电视屏幕的会议室。存储序列号等并不重要;只有数量很重要。
I want to avoid having to re-input the same information over and over again (TV, plasma, 1920x1080, 50", etc etc), so I'd like to simply have, for example, a Televisions table which contains one of each model, and then link those entries to a particular entry in the Rooms table, once for each instance of that item. I end up with something like this:
我想避免不得不一遍又一遍地重新输入相同的信息(电视,等离子,1920x1080,50等等),所以我想简单地说,例如,一个电视表,其中包含一个模型,然后将这些条目链接到Rooms表中的特定条目,对于该项的每个实例一次。我最终得到如下内容:
public class Television
{
public int ID { get; set; }
// ...
// keep track of rooms containing this model
public virtual ICollection<Room> Rooms { get; set; }
}
public class Room
{
public int ID { get; set; }
// ...
public virtual ICollection<Television> Televisions { get; set; }
}
The problem is that, with this setup, you can really only link each row of the Televisions table to a row of the Rooms table once, whereas I'd like to link it, for example, 3 times. What's the best way to accomplish this?
问题是,通过这种设置,您实际上只能将一次Televisions表的每一行链接到Rooms表的一行,而我想将它链接起来,例如3次。实现这一目标的最佳方法是什么?
1 个解决方案
#1
0
If you want to make Television and Room with many to many relationship, you need to use join table for them. For example create join table call TelevisionRooms, and create quantity property in the join table. It should solve the problem. See below:
如果你想让电视和房间有多对多关系,你需要使用连接表。例如,创建连接表调用TelevisionRooms,并在连接表中创建数量属性。它应该解决问题。见下文:
public class Television
{
public int TelevisionID { get; set; }
public int ModelID { get; set; }
public virtual ICollection<Room> Rooms { get; set; }
}
public class Room
{
public int RoomID { get; set; }
//
public virtual ICollection<Television> Televisions { get; set; }
}
public class TelevisionRoom
{
public int TelevisionID { get; set; }
public int RoomID { get; set; }
public int Quantity { get; set; }
}
public class Model
{
public int ModelID { get; set; }
public string ModelName { get; set; }
}
Hope it will help.
希望它会有所帮助。
#1
0
If you want to make Television and Room with many to many relationship, you need to use join table for them. For example create join table call TelevisionRooms, and create quantity property in the join table. It should solve the problem. See below:
如果你想让电视和房间有多对多关系,你需要使用连接表。例如,创建连接表调用TelevisionRooms,并在连接表中创建数量属性。它应该解决问题。见下文:
public class Television
{
public int TelevisionID { get; set; }
public int ModelID { get; set; }
public virtual ICollection<Room> Rooms { get; set; }
}
public class Room
{
public int RoomID { get; set; }
//
public virtual ICollection<Television> Televisions { get; set; }
}
public class TelevisionRoom
{
public int TelevisionID { get; set; }
public int RoomID { get; set; }
public int Quantity { get; set; }
}
public class Model
{
public int ModelID { get; set; }
public string ModelName { get; set; }
}
Hope it will help.
希望它会有所帮助。