Save a Business Object with a EditableRootList

Jan 25, 2011 at 8:48 PM

In EF I have set the table as a EditableRootList. Now I try to save the changes, but get the following exception:

Can not directly save a child object

Here is the code:

   var emp = Business.Objects.EmployeeList.Get(id);
   emp.UserName = "New Value";
   emp.Save();   // Exception is thrown

I know, that since I am using an EditableRootList, this is suppost to happen. So I was hopping I could do something like this:

   var list = Business.Objects.UserAccountList();
   list.Add(emp);
   list.Save();

But there is no default constructor. So is there a way to save this object.

Thankx, Cheers Harry

Jan 25, 2011 at 10:23 PM

Hi Harry,

CSLA classes should use factory methods and that's why the default constructor is marked as private. You should use factory method New:

 

var list = Business.Objects.UserAccountList.New();

~Josip

Jan 26, 2011 at 12:32 PM

Thankx Josip, this did help alot!

By using CLSA, or in particular CSLA Extension, do you think that it would be desirable to have an EditableRoot and some kind of list generated, or the other way around as we see it in EditableRootList, where we have a list with an "entity" generated, but with enhanced with more "EditableRoot" functionality?

I read comments where the community is greating views in the database and then generate CLSA objects from them. Is this the way to go? For our product I can see some benefits in some area, in others it would just be an overhead...

Cheers Harry

Jan 26, 2011 at 1:11 PM

This issue has been discussed few times here. Generally I recommend you to split your objects in two EF models, one for editing main business logic MainModel: (Orders, Transactions etc) and one for editing common data: CommonModel (Customers, Transaction Types etc). That way you can add common data to the MainModel as read-only list objects for lookup lists. I have finished one large project for my company using this approach and it works very well.

If you need any help with your project setup feel free to ask.

~Josip