Having column(s) automatically set in all entities when saved.

Posts   
 
    
bluebird
User
Posts: 2
Joined: 16-Feb-2010
# Posted on: 16-Feb-2010 14:59:49   

Hi,

I just inherited an existing project and I am a newbie to LLBLGen. I am using version 2.6 of LLBLGen (Adapter template group), SQL Server 2005, and .net Studio 2005. My problem is that I have 100+ tables in my database and 100+ LLBLGen entity classes and I want to add the field LastModifiedBy to all the tables. I can add the field to the tables in the database and generate new LLBLGen entity classes, but I don’t want to have to touch each generated entity class to have it set the LastModifiedBy field to the logged in user id. Is there a way to use a custom made template (or some other, better way) to add code to all the entities classes so that on save all the entity classes can get the logged in user id from one general spot and set its own LastModifiedBy field?

MTrinder
User
Posts: 1461
Joined: 08-Oct-2008
# Posted on: 16-Feb-2010 21:52:29   

I do exactly this on pretty much everything I build with LLBLGen by adding the following code to the

// __LLBLGENPRO_USER_CODE_REGION_START CustomDataAccessAdapterCode

region of the DataAccessAdapter class file.


protected override void OnBeforeEntitySave(IEntity2 entitySaved, bool insertAction)
        {
            string userName = WindowsIdentity.GetCurrent().Name.ToString();
            if (entitySaved.Fields["CreatedDate"] != null)
            {
                if (entitySaved.Fields["CreatedDate"].CurrentValue == null)
                {
                    entitySaved.Fields["CreatedDate"].CurrentValue = DateTime.Now;
                }   
            }

            if (entitySaved.Fields["CreatedBy"] != null)
            {
                if (entitySaved.Fields["CreatedBy"].CurrentValue == null)
                {
                    entitySaved.Fields["CreatedBy"].CurrentValue = userName;
                }
            }
            if (entitySaved.Fields["ModifiedDate"] != null)
            {
                entitySaved.Fields["ModifiedDate"].CurrentValue = DateTime.Now;
            }
            if (entitySaved.Fields["ModifiedBy"] != null)
            {
                entitySaved.Fields["ModifiedBy"].CurrentValue = userName;
            }
            

        }

Matt

bluebird
User
Posts: 2
Joined: 16-Feb-2010
# Posted on: 16-Feb-2010 22:01:53   

Thanks for the quick response. This looks like just what I need I will give it a try.

Keith

MTrinder
User
Posts: 1461
Joined: 08-Oct-2008
# Posted on: 16-Feb-2010 22:20:29   

No problem, always happy to help.

Matt

MTrinder
User
Posts: 1461
Joined: 08-Oct-2008
# Posted on: 16-Feb-2010 22:22:49   

BTW, the documentation for this is here

Matt