表外键/自身关联?
我有一个表,用于存储商店的库存,该表看起来像这样
InventoryId int
ParentId int
ShortDesc int
...
[other product data]
...
T 恤将存在于表中,其 ParentId 为 -1。尺寸和颜色的任何变化都将与 ParentID 字段中的原始父 InventoryID 存在于同一表中。
EG
InventoryID |ParentID |ShortDesc
----------------------------------------------------
111 -1 Skull TShirt
112 111 Skull TShirt Black Small
113 111 Skull TShirt Black Medium
114 111 Skull TShirt Black Large
115 111 Skull TShirt White Small
我是否可以指定某种外键或关联,当我将此数据库与实体框架一起使用时,我可以在该实体上拥有一个导航属性,该属性将列出库存实体的所有子库存?
I've got a table that stores inventory for a store that looks like this
InventoryId int
ParentId int
ShortDesc int
...
[other product data]
...
A TShirt will exist in the table with a ParentId of -1. Any variations of size and colour will exist in the same table with the original parent InventoryID in the ParentID field.
EG
InventoryID |ParentID |ShortDesc
----------------------------------------------------
111 -1 Skull TShirt
112 111 Skull TShirt Black Small
113 111 Skull TShirt Black Medium
114 111 Skull TShirt Black Large
115 111 Skull TShirt White Small
Is it at all possible that I could specify some kind of foreign key or association where when I use this DB with the Entity Framework I can have a Navigation property on this Entity that will list all of an Inventory Entity's child Inventory?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,这很好用。作为一个实际示例,使用默认设置创建 Northwind 数据库的模型,并查看员工主管关系。实体框架不会自动获取正确的 Supervisor 属性名称,但关联本身可以工作,并且您可以手动重命名该属性。
关于您的具体示例,我对
-1
表示怀疑。如果这是真实库存记录的真实主键,那很好。如果它是一个表示没有记录的神奇值,则可以使用 NULL 或使其成为真正的记录。为了让实体框架识别这种关系,您将需要创建一个真正的数据库外键,因此所有值都必须实际引用记录。Yes, this works just fine. For a real-world example, make a model of the Northwind database with default settings, and look at the employee supervisor relationship. The Entity Framework will not automatically get the Supervisor property name right, but the Association itself works, and you can rename the property manually.
Regarding your specific example, I am suspicious of the
-1
. If this is a real primary key of a real inventory record, fine. If it's a magic value indicating no record, then either use NULL or make it a real record. In order to get the Entity Framework to recognize this relationship, you're going to need to make a real database foreign key, so all values must actually refer to records.