我有两个表:Products(ProductID, ProductName)
和 之间的Categories(CategoryID, CategoryName)
多对多关系Products
Categories
ProductsCategories(ProductID, CategoryID)
如何实现一个视图,使用户能够使用 Asp.Net Mvc 和 LINQ to SQL添加和编辑具有多个类别的产品?
我有两个表:Products(ProductID, ProductName)
和 之间的Categories(CategoryID, CategoryName)
多对多关系Products
Categories
ProductsCategories(ProductID, CategoryID)
如何实现一个视图,使用户能够使用 Asp.Net Mvc 和 LINQ to SQL添加和编辑具有多个类别的产品?
Since LINQ to SQL only supports a strict 1:1 mapping with your database you would have to make a link table. This would look something like...
[ProductCategories]
ID
ProductID (FK)
CategoryID (FK)
Then for every relationship of product to categories you would just make a new ProductCategories entity and insert it. So given the data...
Products
ID Name
1 Apple
2 XBOX 360
Categories
ID Name
1 Produce
2 Electronics
3 Game Systems
Your link table would look like
ProductCategories
ID ProductID CategoryID
1 1 1
2 2 2
3 2 3