1-n 问题建模
我有一个如下所示的数据库:
Product: PK(ProductId), ProductName.......
Store: PK(StoreId), StoreName......
ProductStore: PK(ProductId, StoreId), Price, Quantity
这里我在 Product
和 ProductStore
之间有 1-many,在 Store
和 Store
之间也有 1-many代码>ProductStore。当我创建模型时,我基本上有类似的东西
Product.ProductStores
,这在编码 UI 时会产生问题(例如数据绑定)。当我使用模型时,查询数据将始终仅返回 ProductStores
集合中的一个 ProductStore
,因为我总是一次使用一个商店 (PK:ProductID+StoreId) 。我还需要为商店保留通用的产品表,因此需要 ProductStoreTable
。
有没有办法可以在模型 1->0..1 中表示 Product->ProductStore,它实际上在数据库 1->many 中?
谢谢, 戈兰
I have a database that looks like this:
Product: PK(ProductId), ProductName.......
Store: PK(StoreId), StoreName......
ProductStore: PK(ProductId, StoreId), Price, Quantity
Here I have 1-many between Product
and ProductStore
, and also 1-many between Store
and ProductStore
. When I create model I basically have something like
Product.ProductStores
and this creates a problem when coding UI (example would be databinding). WHen I work with model, Querying data will always return only one ProductStore
in ProductStores
collection, because I am always working with one store at a time (PK:ProductID+StoreId). I also need to ave common Product table for Stores, therefore the necessity for ProductStoreTable
.
Is there a way I can represent Product->ProductStore in model 1->0..1, which is actualy in database 1->many?
Thanks,
Goran
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我通常通过在 Product 上创建一个名为 ProductStore 的 Porperty 来处理这个问题,它只返回 this.ProductStores.FirstOrDefault()。并在集合上清除 this.ProductStores 并添加新的(如果它们不相同)。
I usually deal with this by creating a Porperty on the Product called ProductStore that simply returns this.ProductStores.FirstOrDefault(). and on the set clears this.ProductStores and adds the new one, if they are not the same.