Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Mark 119 posts 252 karma points
    May 12, 2020 @ 12:23
    Mark
    0

    Subscribing to Npoco database events

    Hey guys,

    Sorry if this is in the docs, I have done a bit of searching and have not found an answer as yet...

    I am inserting/updating (or at least will be when this works!) data to custom database tables as per the documentation, but I want to subscribe to Npoco database events (OnInserting, OnUpdating, etc.) so that I can auto-populate things like timestamp fields.

    The example from Npoco shows (https://github.com/schotime/NPoco/wiki/Performing-Operations-during-Poco-events-(Insert,-Update,-etc)):

    public class DatabaseEvents : Database
    {
        public DatabaseEvents(DbConnection connection) : base(connection)
        {
        }
    
        protected override bool OnInserting(InsertContext insertContext)
        {
            if (insertContext.Poco is CommonEntity entity)
            {
                entity.DateInserted = DateTime.UtcNow;
            }
    
            return base.OnInserting(insertContext);
        }
    
        protected override bool OnUpdating(UpdateContext updateContext)
        {
            var entity = updateContext.Poco as CommonEntity;
            if(entity != null)
            {
                entity.DateModified = DateTime.UtcNow;
            }
    
            return base.OnUpdating(updateContext);
        }
    }
    

    But I am pretty sure I need to register this with Umbraco somehow as it doesn't work out of the box...

    Has anyone done this yet with V8, if so, please help! :-)

    Many thanks,

    Mark

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies