I want to fluently map a many-to-many(technically two one-to-many)
relationships between products and categories. A product can be
associated with many categories and a category can be associated with
many products.
The tables are as follows(I did not design these>.<):
catalog(holds product definitions):
ID PK AI
product_id
other descriptive fields...
categories(holds category information):
pkid PK AI
pick_id(the actual id associated with a category)
other descriptive fields....
category_tree(holds the relationships between a product and multiple
categories)
ID PK AI
product_id(which corresponds to the catalog table product_id but there
isn't any real foreign keys defined in the field)
category_id( which actually corresponds to pick_id from the categories
table; again no real foreign keys defined)
other descriptive fields...
Since there are other fields in the category_tree table I creating a
ProductCategories entity to still be able to hold the other values
that are in this table. Both entities Product and Category have an
IList<product>,IList<Category> to hold their equivalent associations.
All mappings are as follows:
Product Mapping
//Associated Categories for this product
HasMany<ProductCategories>(x => x.ProductCategories)
.Table("category_tree")
.KeyColumn("product_id");
Other fields excluded
Category Mapping
HasMany<ProductCategories>(x => x.ProductCategories)
.Table("category_tree")
.KeyColumn("category_id");
ProductCategories
Table("category_tree");
Id(x => x.Id)
.GeneratedBy.Native();
References(x => x.Product)
.Column("product_id");
References(x => x.Category)
.Column("category_id");
Map(x => x.ProductType)
.Column("type");
Map(x => x.FacilityCode)
.Column("fac_code");
I am using it in this context:
var products = repo.getAllByFacilityCode(facCode);
foreach (var item in products)
{
if (!string.IsNullOrEmpty(item.ProductID))
{
for (int i = 0; i <
item.ProductCategories.Count; i++)
{
if (item.ProductCategories.Count > 0)
{
Console.WriteLine(item.ProductCategories[i].Category.CategoryName);
}
else
{
Console.WriteLine("No Categories
Associated with {0}", item.ProductID);
}
}
}
};
I know I am missing an Inverse, cascade etc, but I just wanted to get
it working so I could view associated categories while going through
products. The problem I am running into is it is not even finding
entries for some product_ids and the one that do have the List
populated, do not have the correct entries from the DB.
I get "No row with the given identifier
exists[RLWSDomain.Category#4191" which is not a category_id in the
category_tree table for the given product_id.
I have an inkling that it might be the mismatch names but I am not
sure. Is this scenario even possible to fluently map or am I missing
something terribly obvious?
Any direction would be great and I thank anyone in advance.
--
You received this message because you are subscribed to the Google Groups
"nhusers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/nhusers?hl=en.