Cloning all fields except the primary key?

Posts   
 
    
stevenpack
User
Posts: 4
Joined: 05-Mar-2007
# Posted on: 14-Mar-2007 11:43:41   

Hi All,

Mode is Adapter.

I have 2 tabs.

Business Contact with a ContactUserControl Operations Contact with a ContactUserControl

Each control has some text fields and the operations tab also has a checkbox called "Same as Business Contact". The control also has a DataSource property of type ContactEntity

On checking the checkbox (which is on the form), i call the following code:


businessContact.PropertyChanged += new PropertyChangedEventHandler(targetControl.MasterPropertyChanged);

In the event handler on the user control, I would ideally do this:


        public void MasterPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            ContactEntity master = sender as ContactEntity;
            if (master != null)
            {
                _dataSource.Fields = master.Fields.CloneAsDirty();
                BindDataSource();
            }
        }



Where data source is of type ContactEntity. The problem with that is that it will also update the ContactId field, which is primary key. That in turn leads to SQL being generated that attempts to update the primary key, which is not allowed as it is an identity column.

I tried setting the primary key back to its orginal value after cloning the fields, but that doesn't work either - presumably because the field is already marked as dirty.

I also tried only updating the field that had changed, as in:


_dataSource.Fields[e.PropertyName].CurrentValue = master.Fields[e.PropertyName].CurrentValue;

That doesn't seem to mark the fields as dirty for one, and is not as useful to me, as I'd rather update everything in one fell swoop.

So, the question is, besides explicitly setting every single value manually, as in:


_dataSource.FirstName = master.FirstName;
_dataSource.LastName = master.LastName;
_dataSource.Mobile = master.Mobile;

...and so on

Or perhaps, setting each field that doesn't exist in Entity.Fields.PrimaryKeyFields, is there any efficient way to clone one entity to another, excluding the primary key fields already built in to the object model?

Regards, Steve Pack

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 14-Mar-2007 13:46:52   

Your first approach was the better one, just use the following:


        public void MasterPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            ContactEntity master = sender as ContactEntity;
            if (master != null)
            {
                _dataSource.Fields = master.Fields.CloneAsDirty();
                _dataSource.ContactId.IsChanged = false;
                BindDataSource();
            }
        }

stevenpack
User
Posts: 4
Joined: 05-Mar-2007
# Posted on: 14-Mar-2007 15:14:03   

That will still actually change the primary key, but mark it as unchanged. I guess that would work, but I don't like the fact it will be changed.

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 14-Mar-2007 16:27:59   

The PK won't be saved. That's if you want to use the "Fields.CloneAsDirty();" method.

Still you can manually copy the data field by field, and set each field's IsChanged to true, without copying the PK.