LoadProperty after Insert issue

LoadProperty after Insert issue

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


DaseinFiasco posted on Wednesday, February 20, 2008

Greetings,

I'm having an issue with Csla 3.0 during database inserts when I try to return and assign a new primary key. Here're the relevant portions code I'm using:

First, from DataPortal_Insert():

int? id = null;
ctx.DataContext.AddMyObject(
                    ReadProperty<string>( NameProperty ),
                    ReadProperty<string>( AddressProperty ),
                    ReadProperty<string>( CityProperty ),
                    ReadProperty<string>( StateProperty ),
                    ReadProperty<string>( PostalCodeProperty ),
                    ref id );

When that code executes, the id variable returns with a correct value coresponding to the new PK in the database. Now I want to immediately assign that value to the object's id field like so (I've tried both):

            LoadProperty<int>( IdProperty, id.GetValueOrDefault( 0 ) );
or
            SetProperty<int>( IdProperty, id.GetValueOrDefault( 0 ) );

For what it's worth, here's the field declaration itself:

        private static PropertyInfo<int> IdProperty = RegisterProperty<int>( typeof( MyObject), new PropertyInfo<int>( "Id" ) );
        [System.ComponentModel.DataObjectField( true, true )]
        public int Id
        {
            get { return GetProperty<int>( IdProperty ); }
        }

The problem is that within the application which is invoking .Save() on MyObject, the .Id property remains unset. Interestingly enough, during the DataPortal_Insert() method, I can add a watch to "this.Id" and it's set correctly. Only having returned to the application does the .Id property show up as its default value of 0. Any ideas?

Thanks,

.t.



JoeFallon1 replied on Wednesday, February 20, 2008

This is the number one problem people have when coding CSLA.

In the UI code you probably have this:

myBO.Save

You need to have this (at a minimum):

myBO = myBO.Save

By updating the reference, you will see the correct value back in the UI.

Remember CSLA returns a new object to the UI.

Joe

DaseinFiasco replied on Wednesday, February 20, 2008

The number one problem? I'm sure people interpret it in all sorts of interesting ways. :) It's pretty mysterious until you realize just how stupid you are. In any case, thanks for the explanation; references are in good shape now and code is working!

.t.

Copyright (c) Marimer LLC