tags:

views:

59

answers:

2

I have couple tables in my schema with PK and FK relationship. I have created the DAL using SubSonic generator.

If I create new parent and its childs, how should I save both? separately or in one shot?

e.g.

Parent.Save();  
ChildCollection.SaveAll();

I tried above, but it does not work because ChildCollection does not have its parent's Id. Is it that I have to assign parent Ids for each child my self or is there an option to save it all in one shot?

please advise...

A: 

You can use partial classes with custom overload of save function to provide the desired functionality.

public partial class Class1
{
    public void Save(bool childern)
    {
        Save();

        if (childern)
        {
            //based on primary/foreign key SubSonic provides
            //methods to fetch child collections related to
            //this (primary key) table.
            ChildernCollection col = Childerns();

            col.SaveAll();
        }
    }
}
TheVillageIdiot
A: 

Assumption- I am assuming that your Primary Keys are auto generated by your Data Base. if so what you will need to do is first Save the parent and then populate the ParentID property in each of the objects in your Children Collection. Once you have done that you will be able to Save your Children collection.

        Parent.Save();
        ChildCollection.ForEach(x => x.ParentID = Parent.ParentID);
        ChildCollection.SaveAll();
runxc1 Bret Ferrier