Trouble persisting information

Posts   
 
    
gj1118
User
Posts: 30
Joined: 21-Jul-2010
# Posted on: 12-Aug-2010 06:57:18   

I am current using LLBLGen version 3.0

i am trying to work with relations across two tables . The concerened tables are Category and RelatedCategory. Category has a one to many relation with RelatedCategory.

I am using this code to deep save my CategoryEntity


CategoryEntity category;
                if (_currentCategoryId == null)
                {
                    category = new CategoryEntity();
                }
                else
                {
                    category = new CategoryEntity((int) _currentCategoryId);
                }
                category.CategoryName = txtCategoryName.Text.Trim();
                category.CategoryNotes = txtCategoryNote.RichText;
                category.IsDeleted = radCheckBoxIsDeleted.Checked;
                category.ParentCategoryId = _currentParentId;
                
                category.RelatedCategories.DeleteMulti();

                foreach (var i in RelatedCategoryId)
                {
                    RelatedCategoryEntity relatedCategory = category.RelatedCategories.AddNew();
                    relatedCategory.CategoryId = i;
                }

                category.Save(true);

but unfortunately, the code above is not saving the RelatedCategoryInformation.

please see the attached database diagram !

Somebody,some help please!

Thanks and Regards Gj

Attachments
Filename File size Added on Approval
category_relatedCategories.JPG 15,613 12-Aug-2010 06:57.44 Approved
Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 12-Aug-2010 08:36:38   

Please try the following code:

                foreach (var i in RelatedCategoryId)
                {
                    var relatedCategory = new  RelatedCategoryEntity(i);                
                    category.RelatedCategories.Add(relatedCategory);
                }

Hint: don't use AddNew(), it's there for databinding purposes only.

gj1118
User
Posts: 30
Joined: 21-Jul-2010
# Posted on: 12-Aug-2010 09:30:24   

Thanks Walaa for your help

but I am not sure why, the data is still not being persisted. here is the updated code


                CategoryEntity category;
                if (_currentCategoryId == null)
                {
                    category = new CategoryEntity();
                }
                else
                {
                    category = new CategoryEntity((int) _currentCategoryId);
                }
                category.CategoryName = txtCategoryName.Text.Trim();
                category.CategoryNotes = txtCategoryNote.RichText;
                category.IsDeleted = radCheckBoxIsDeleted.Checked;
                category.ParentCategoryId = _currentParentId;
                
                category.RelatedCategories.DeleteMulti();

                foreach (var i in RelatedCategoryId)
                {
                    var relatedCategory = new RelatedCategoryEntity(i);
                    category.RelatedCategories.Add(relatedCategory);
                }

                category.Save(true);

For generating the code, I selected the default options in LLBLGEN

Thanks once again!

gj1118
User
Posts: 30
Joined: 21-Jul-2010
# Posted on: 12-Aug-2010 09:39:48   

After some more testing I found out an anomaly whenever i an trying to save category without a related category the category is being persisted. but when i try to save a category which has related categories, the category is not being persisted. there are no errors that are returned in the latter case

thanks

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 12-Aug-2010 09:40:53   
            foreach (var i in RelatedCategoryId)
            {
                var relatedCategory = new RelatedCategoryEntity(i);             
                category.RelatedCategories.Add(relatedCategory);
            }

RelatedCategoryId Contain IDs of relatedategory entities already existing in the database, right?

gj1118
User
Posts: 30
Joined: 21-Jul-2010
# Posted on: 12-Aug-2010 09:44:42   

Thanks for your post

RelatedCategoryId Contain IDs of relatedategory entities already existing in the database, right?

Yes, I have debugged that , and I found that the relatedcategoryId has the list of selected categories.

P.S.

RelatedCategoryId is a list of ints. I use this list to store the selected relatedCategorie's ID

A relatedcategory is basically a category

I have uploaded a screenshot from my LLBLGEN . I made sure that LLBLGEN detects the relations between the two tables

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 12-Aug-2010 09:48:06   

I've looked at the database diagram, I guess the code should be as follows:

                foreach (var i in RelatedCategoryId)
                {
                    var relatedCategory = new RelatedCategoryEntity();
                    relatedCategory.RelatedCategoryId = i;
                    category.RelatedCategories.Add(relatedCategory);
                }

You are a record in the intermediate table to join between 2 Categories, right. So CategoryId should be the id of the Category entity being inserted. And the RelatedCategoryId is the Id taken from the RelatedCategoryId list.

Am I right?

gj1118
User
Posts: 30
Joined: 21-Jul-2010
# Posted on: 12-Aug-2010 09:50:40   

Walaa wrote:

I've looked at the database diagram, I guess the code should be as follows:

                foreach (var i in RelatedCategoryId)
                {
                    var relatedCategory = new RelatedCategoryEntity();
                    relatedCategory.RelatedCategoryId = i;
                    category.RelatedCategories.Add(relatedCategory);
                }

You are a record in the intermediate table to join between 2 Categories, right. So CategoryId should be the id of the Category entity being inserted. And the RelatedCategoryId is the Id taken from the RelatedCategoryId list.

Am I right?

That works... AWESOME simple_smile

thanks

I have marked this post as fixed!!! thanks Walaa