When I insert a record or update, I would like to not only send those changes to DB, but also send create/update realted user and datetime information.
What would be the best approach for this kind of requriement?
If it is possible, I want to maange it at the application side instead of implementing it in DB using triggers or stored procedures.
I’ve implemented the code “ A Method to Handle Audit Fields in LINQ to SQL” mentioned above and it works perfectly for “string” values.
When I try referencing a “date” column, I keep getting the following error:
'DataRecord' does not implement interface member 'IAuditable.ChangedDate'. 'DataRecord.ChangedDate' cannot implement 'IAuditable.ChangedDate' because it does not have the matching return type of 'System.DateTime'.
I’ve tried different date types in SQL 2008 (date / datetime / datetime2). I’ve tried changing the type in the interface to different data types (System.Data.SqlTypes.SqlDateTime). I’ve even tried converting between date types in “ProcessAuditFields” but
with no luck.
I’m probably doing something really silly, but I just can’t seem to find it.
I have a table with multiple columns, including 2 for audit. The Audit columns are called:
ChangedDate (datetime) and ChangedUser (nvarchar(50). Code:
I treat both INSERTS and UPDATES the same, so I haven’t changed to code yet (to keep as close to the sample until I get it to work). Will tidy up later.
gijigae
Member
26 Points
56 Posts
CreatedDateTime, CreatedBy, UpdatedDateTime and UpdatedBy
Nov 12, 2010 03:08 PM|LINK
Hi All,
When I insert a record or update, I would like to not only send those changes to DB, but also send create/update realted user and datetime information.
What would be the best approach for this kind of requriement?
If it is possible, I want to maange it at the application side instead of implementing it in DB using triggers or stored procedures.
Thank you all for your great support,
sjnaughton
All-Star
27318 Points
5458 Posts
MVP
Re: CreatedDateTime, CreatedBy, UpdatedDateTime and UpdatedBy
Nov 12, 2010 10:28 PM|LINK
Hi Gijigae, have a look at this A Method to Handle Audit Fields in LINQ to SQL I use this all the time for Linq to SQL I also have a variation for EF4.
Dynamic Data Audit Fields
Always seeking an elegant solution.
gijigae
Member
26 Points
56 Posts
Re: CreatedDateTime, CreatedBy, UpdatedDateTime and UpdatedBy
Nov 13, 2010 03:37 PM|LINK
Hi Steve,
Thank you very much for the link.
Very good article on the auditing handling.
There is section where partial classes implement IAuditable interface.
Where should this implement take place?
Would it be something that can be included in a metadata class as below?
[MetadataType(typeof(CustomerMetadata))] partial class Customer : IAuditable { partial class CustomerMetadata { internal Object CustomerID { get; set; } internal Object CompanyName { get; set; } internal Object ContactName { get; set; } internal Object ContactTitle { get; set; }Best regards,
sjnaughton
All-Star
27318 Points
5458 Posts
MVP
Re: CreatedDateTime, CreatedBy, UpdatedDateTime and UpdatedBy
Nov 14, 2010 10:28 AM|LINK
Hi Gijigae, that is correct, you apply it to each entity on the partial class.
Dynamic Data Audit Fields
Always seeking an elegant solution.
erwee
Member
9 Points
6 Posts
Re: CreatedDateTime, CreatedBy, UpdatedDateTime and UpdatedBy
Nov 30, 2011 09:56 AM|LINK
Hi Steve
I’ve implemented the code “ A Method to Handle Audit Fields in LINQ to SQL” mentioned above and it works perfectly for “string” values. When I try referencing a “date” column, I keep getting the following error:
'DataRecord' does not implement interface member 'IAuditable.ChangedDate'. 'DataRecord.ChangedDate' cannot implement 'IAuditable.ChangedDate' because it does not have the matching return type of 'System.DateTime'.
I’ve tried different date types in SQL 2008 (date / datetime / datetime2). I’ve tried changing the type in the interface to different data types (System.Data.SqlTypes.SqlDateTime). I’ve even tried converting between date types in “ProcessAuditFields” but with no luck.
I’m probably doing something really silly, but I just can’t seem to find it.
I have a table with multiple columns, including 2 for audit. The Audit columns are called: ChangedDate (datetime) and ChangedUser (nvarchar(50). Code:
internal interface IAuditable { string ChangedUser { get; set; } DateTime ChangedDate { get; set; } } private static void ProcessAuditFields(IList<System.Object> list, bool UpdateCreatedFields) { foreach (var item in list) { IAuditable entity = item as IAuditable; if (entity != null) { if (UpdateCreatedFields) { entity.ChangedUser = GetUserName(); entity.ChangedDate = DateTime.Now; } entity.ChangedUser = GetUserName(); entity.ChangedDate = DateTime.Now; } } }I treat both INSERTS and UPDATES the same, so I haven’t changed to code yet (to keep as close to the sample until I get it to work). Will tidy up later.
Do you have any ideas?
Many thanks
sjnaughton
All-Star
27318 Points
5458 Posts
MVP
Re: CreatedDateTime, CreatedBy, UpdatedDateTime and UpdatedBy
Dec 03, 2011 12:07 PM|LINK
Hi Erwee, the type is datatime in the DB you then add the interface to each entity in you buddy classes like this:
[MetadataTypeAttribute(typeof(Category.CategoryMetadata))] public partial class Category : IAuditable { internal sealed class CategoryMetadata { [ScaffoldColumn(false)] public int CategoryID { get; set; } public string CategoryName { get; set; } public string UpdatedBy { get; set; } public DateTime UpdatedOn { get; set; } public string CreatedBy { get; set; } public DateTime CreatedOn { get; set; } public EntityCollection<Product> Products { get; set; } } }Always seeking an elegant solution.