I have 3 tables Product (m:1) ProductVsategory (1:n) Category
I think the Product in the above statement refers to SubscriptionProduct in the code correct?
It works fine when the entity already exists but fails when the entity IsNew.
Here too, I think you mean by entity the SubscriptionProductEntity, correct?
Also, what exactly does "fails" mean, what are the symptoms of failure?
If what I understands so far is correct, then I think you should be adding the NEW SubscriptionProductEntity into the UOW as follows:
foreach (int categoryid in categories)
{
ProductVsCategoryEntity category = new ProductVsCategoryEntity();
category.Product = entity;
category.CategoryId = categoryid;
category.IsNew = true;
work.AddForSave(category, null, false, true);// true here for recurse, this should add the SubscriptionProductEntity to the UOW
}
Another way of doing it is as follows:
foreach (int categoryid in categories)
{
ProductVsCategoryEntity category = new ProductVsCategoryEntity();
category.Product = entity;
category.CategoryId = categoryid;
category.IsNew = true;
entity.ProductCategories.Add(category);
}
work.AddForSave(entity, null, false, true);