Database: Difference between revisions

From Bitnami MediaWiki
Jump to navigation Jump to search
No edit summary
No edit summary
 
(One intermediate revision by the same user not shown)
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:  
Line 9: Line 9:
,verions integer null</pre>
,verions integer null</pre>


<strong>NOTE:</strong> The audit columns must be the last 5 columns of the table at all times. 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.
<strong>NOTE:</strong> 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.
 
<strong>Audit fields (C# code).</strong> In the Novus.Model.Entity.PartialEntities folder, create a partial class for the entity and implement the AuditFields interface e.g.


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
public partial class Foo : AuditFields
In the C# code, set the fields like this:
In the C# code, set the fields like this:
entity.CreatedBy = GetUserId();
entity.CreatedBy = GetUserId();
entity.CreatedOn = DateTime.UtcNow;
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.
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.
 
<strong>Audit fields (BaseService helper method).</strong> 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.




<strong>Return to [[Programming Guide]]</strong>
<strong>Return to [[Programming Guide]]</strong>

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