Consider the following class:
class Item
{
public int ID;
public string Name;
public IList Categories; //this is a collection of strings--category names
}
and the database schema:
CREATE TABLE Item(ID int NOT NULL, Name varchar(50) NOT NULL, CONSTRAINT PK_Item PRIMARY KEY CLUSTERED (ID ASC))
CREATE TABLE Category(ID int NOT NULL, Name varchar(50) NOT NULL, CONSTRAINT PK_Category PRIMARY KEY CLUSTERED (ID ASC))
CREATE TABLE ItemCategory(ItemID int, CategoryID INT)
ALTER TABLE [dbo].[ItemCategory] WITH CHECK ADD CONSTRAINT [FK_ItemCategory_Category] FOREIGN KEY([CategoryID])
REFERENCES [dbo].[Category] ([ID])
ALTER TABLE [dbo].[ItemCategory] CHECK CONSTRAINT [FK_ItemCategory_Category]
ALTER TABLE [dbo].[ItemCategory] WITH CHECK ADD CONSTRAINT [FK_ItemCategory_Item] FOREIGN KEY([ItemID])
REFERENCES [dbo].[Item] ([ID])
ALTER TABLE [dbo].[ItemCategory] CHECK CONSTRAINT [FK_ItemCategory_Item]
In short, there are three tables: Item, Category, and the association table ItemCategory.
The Item class has a collection of category names (strings), but NOT category objects.
What is the correct mapping in this situation? I know how to create the mapping with objects, but not with plain strings. In other words, I don't want Categories to be treated as objects, but rather just strings.
<class name="MyAssembly.Item, MyAssembly" table="Item">
<id name="ID" column="ID" type="Int32">
<generator class="identity" />
</id>
<property name="Name" column="Name" type="String" length="50"/>
<bag name="Categories" table="ItemCategory">
<key column="ItemID"/>
<many-to-one column="CategoryID" ??? /> <!--this should retrieve Category.Name from table Category-->
</bag>
</class>
|