UoW ... again

Posts   
 
   
 
Anonymous
User
Posts: 0
Joined: 11-Nov-2006
# Posted on: 06-Jun-2007 10:30:19   

Adapter. V 2.0.0.0 Latest

Hi guys,

Am building an Entity in memory and tracking the changes in a UnitOfWork object. (Is for a farily complex wizard where all the data must be processed together). I have 3 tables Product (m:1) ProductVsategory (1:n) Category and am using the code below to update the ProductVsCategory relationship. It works fine when the entity already exists but fails when the entity IsNew. I suspect this is because entity.ProductId does not have a value. Is there anyway around this? Once I get this working, I'll be done. So as you can imagine, quite important to me simple_smile

Cheers,


            public void UpdateProductCategoryEntity(List<int> categories)
            {
                UnitOfWork2 work = ViewCache[UowKey] as UnitOfWork2;
                SubscriptionProductEntity entity = ViewCache[DataKey] as SubscriptionProductEntity;

                if (work != null && entity != null)
                {           
                    
                    // create new relationships
                    foreach (int categoryid in categories)
                    {
                        ProductVsCategoryEntity category = new ProductVsCategoryEntity();                   
                        category.ProductId = entity.ProductId;
                        category.CategoryId = categoryid;
                        category.IsNew = true;                      
                        work.AddForSave(category);                      
                    }                                               
                }

                ViewCache[UowKey] = work;
                ViewCache[DataKey] = entity;
            }

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 06-Jun-2007 10:57:49   

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);     
Anonymous
User
Posts: 0
Joined: 11-Nov-2006
# Posted on: 06-Jun-2007 11:14:49   

Thanks Walaa, is working perfick. By using the productid (rather than the product itself) I was failing to make the association need to ensure a recursive call. Is that right?

Cheers

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 06-Jun-2007 11:15:40   

Right simple_smile