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.
According to my knowledge you have to create a class first and in that class override these events and to trigger these events you have to create that class object and return to your repo layer
For example:
public class AppNameDatabase : Database
{
public AppNameDatabase (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);
}
}
At last create a factory class to encapsulate it further
public class DatabaseFactory : IDatabaseFactory
{
readonly DbConnection _connection;
public DatabaseFactory(DbConnection connectionl)
{
_connection=connection;
}
public IDatabase GetDbConnection()
{
using (var dbContext = new AppNameDatabase (_connection)
{
return dbContext;
}
}
}
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)):
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
According to my knowledge you have to create a class first and in that class override these events and to trigger these events you have to create that class object and return to your repo layer For example:
At last create a factory class to encapsulate it further
You can ask if you have any further questions
is working on a reply...