Beginner's question - DataPortal_Insert

Beginner's question - DataPortal_Insert

Old forum URL: forums.lhotka.net/forums/t/7379.aspx


erict2000 posted on Friday, July 31, 2009

Hi -- I'm following the PTracker, and have started my own simple project. I can retrieve lists and objects from the DB and create a new object. When the LINQ returns from the Stored Proc that inserts a new row, I get back the new ID value (identity PK), and put that into _id. Debug shows that it's there. Then I step out of DataPortal_Insert and a whole bunch of CSLA functions until I finally get back to my caller that did thing.Save(). At this point the _id variable is back to 0, instead of the identity value I put in in DataPortal_Insert.
        protected override void DataPortal_Insert()
        {
            using (var ctx = ContextManager.GetManager(o.DAL.Database.DB, false))
            {
                int? newId = null;
                ctx.DataContext.addThing(_name, _location, ref newId);
                _id = System.Convert.ToInt32(newId);
                FieldManager.UpdateChildren(this);
            }
        }

My question: Why doesn't the update of _id in DataPortal_Insert keep the value? Is it a proxy object, and if so, what will transfer the value from the proxy into my object? Thanks for any clues here.
Eric T.

RockfordLhotka replied on Friday, July 31, 2009

This is probably the most commonly asked question (one way or another) asked by newcomers to CSLA .NET.

Notice that Save() returns a value. In fact, it returns the saved object, which is different from the one you started with. So you must use that return value:

_customer = _customer.Save()

The reason for this is explained in the first couple chapters of Expert 2008 Business Objects.

erict2000 replied on Friday, July 31, 2009

Doh -- Thanks!  I had skipped Ch 4 - 16.  Looks like that was a bad move.

Copyright (c) Marimer LLC