建模1-n问题

时间:2011-08-28 08:37:36

标签: .net oop c#-4.0 model

我有一个如下所示的数据库:

Product: PK(ProductId), ProductName.......
Store: PK(StoreId), StoreName......
ProductStore: PK(ProductId, StoreId), Price, Quantity

此处我在ProductProductStore之间有1-many,在StoreProductStore之间也有1-many。当我创建模型时,我基本上有类似

的东西
Product.ProductStores

这会在编写UI时产生问题(例如,数据绑定)。当我使用模型时,查询数据将始终只返回ProductStore集合中的一个ProductStores,因为我一直在使用一个商店(PK:ProductID + StoreId)。我还需要为商店提供公共产品表,因此需要ProductStoreTable

我有没有办法在模型1-> 0..1中代表Product-> ProductStore,这在数据库1->很多?

谢谢, 戈兰

1 个答案:

答案 0 :(得分:0)

我通常通过在名为ProductStore的Product上创建一个只返回this.ProductStores.FirstOrDefault()的Porperty来解决这个问题。并在集合清除this.ProductStores并添加新的,如果它们不相同。