DEV Community

Discussion on: EF Core: How to implement basic Auditing on your Entities

Collapse
 
arminzia profile image
Armin Zia

Thank you. But I have a follow-up question. There's a problem with how you're storing references to the current user. The CreatedBy and ModifiedBy columns store the current user's username (or email address). there are 2 problems with that. first, there are no FK constraints and you can insert any value. secondly, since we don't have an FK navigation , there's no way to Include such properties. Say we're listing a Ticket entity which has a collection of messages posted by different users (clients, admins, etc.). How would we load the related data? The only piece of information we'd have is their usernames and we can't execute tons of GET queries to load user entities for each record. I'm having trouble wrapping my head around this limitation, any ideas are appreciated.

Collapse
 
rickystam profile image
Ricky Stam • Edited

Hi Armin,

In my example I saved the name or email address of the current user only for auditing purposes. I don't expect to do queries based on CreatedBy or ModifiedBy columns. If you have a different use case where you want to actually do queries on these columns there is nothing stopping you from creating these columns as FKs to the Users Table and instead of the Name property you can save the UserId property of the currently logged in user. If you need help with anything you can DM me here or on Twitter.

Thanks,
Ricky