Database: Difference between revisions
No edit summary |
No edit summary |
||
Line 1: | Line 1: | ||
<strong>See also</strong> the Optimistic Concurrency Control section. | <strong>See also</strong> the [[Optimistic Concurrency Control]] section. | ||
<strong>Audit fields (database).</strong> Generally, most tables should have the following audit columns: | <strong>Audit fields (database).</strong> Generally, most tables should have the following audit columns: |
Latest revision as of 20:45, 3 October 2023
See also the Optimistic Concurrency Control section.
Audit fields (database). Generally, most tables should have the following audit columns:
created_on datetime2(7) NULL ,created_by varchar(100) NULL, ,updated_on datetime2(7) NULL, ,updated_by varchar(100) NULL ,verions integer null
NOTE: The audit columns must be the last 5 columns of the table. If a new column is added to the table the new column must be added before the audit columns so the audit columns remain as the last 5 columns in the table.
Audit fields (C# code). In the Novus.Model.Entity.PartialEntities folder, create a partial class for the entity and implement the AuditFields interface e.g.
public partial class Foo : AuditFields
In the C# code, set the fields like this: entity.CreatedBy = GetUserId(); entity.CreatedOn = DateTime.UtcNow; UTC is not used since this complicates the C# code and SQL code where the time has to be converted to current timezone.
Audit fields (BaseService helper method). With the entity implementing the AuditFields interface, call the SetAuditFields(dbContext, entity) for updates. For new entities, call SetAuditFields(dbContext, entity, true) by passing in true so that the CreatedOn and CreatedBy properties are also set.
Return to Programming Guide