Force New

Posts   
 
    
ianvink
User
Posts: 394
Joined: 15-Dec-2006
# Posted on: 12-Jul-2007 16:25:20   

I want to force all the entities in a collection to be dirty and new and then have 1 PK (has multiple PKs) field set specific to a new value

So I do this:

                
foreach (E entity in Collection)
{
      entity.IsNew = true;
      entity.IsDirty = true;
      entity.SetNewFieldValue("ABC", MyABCVal);
}

The SQL generated only does an Insert for the ABC field, ingnoring the rest of the fields

Query: INSERT INTO [xxx].[dbo].[tb_xxx] ([ABC])  VALUES (@ABC)
    Parameter: @ABC : Byte. Length: 1. Precision: 3. Scale: 0. Direction: Input. Value: 1.

ianvink
User
Posts: 394
Joined: 15-Dec-2006
# Posted on: 12-Jul-2007 16:46:03   

It seems that when you mark an Entity with IsNew, the fields also need to be marked as Dirty. I run this Function against the Entity that I set as IsNew and the entity then saves all the fields.

 public static void SetEntityDirty(IEntity entity, bool SetPrimaryKeysDirty)
        {
            entity.IsDirty = true;
            entity.IsNew = true;
            for (int i = 0; i < entity.Fields.Count; i++)
            {
                bool markAsDirty = true;
                if (!SetPrimaryKeysDirty && entity.Fields[i].IsPrimaryKey)
                    markAsDirty = false;
                else if (entity.Fields[i].SourceColumnDbType == 19) //timestamps (DbType 19) as changed
                    markAsDirty = false;
                
                
                //don't mark the primary-keys and timestamps (DbType 19) as changed
                if (markAsDirty)
                {
                    entity.Fields[i].IsChanged = true;
                    if (entity.Fields[i].IsNull)
                    {
                        //reset Null fields
                        entity.SetNewFieldValue(i, null);
                    }
                }
            }
        }
Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 13-Jul-2007 09:34:33   

Yeah, that's it.