Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6224491
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T08:40:57+00:00 2026-05-24T08:40:57+00:00

I was wondering if anyone could offer some input for an implementation of audit

  • 0

I was wondering if anyone could offer some input for an implementation of audit logging for inserts. I need to ensure that it is transactional.

I have an audit DbSet built upon the following POCO:

public class Audit {
    public int Id { get; set; }
    public User User { get; set; }
    public DateTime Created { get; set; }
    public string Type { get; set; }
    public int EntityId { get; set; }
    public string Message { get; set; }
}

I have a DbSet, say Users, that when I do an insert I want to create automatically add an Audit entity in the Audit DbSet. Consider the following code:

//var user = new User();
//user.Created = DateTime.Now;
//user.Username = "testuser";
//user.Password = "testpassword";
//dataContext.Users.Add(user);

var post = new Post();
post.Created = DateTime.Now;
post.Title = "A sample post";
post.Published = true;
post.Body = "Some content goes in here...";

dataContext.Posts.Add(post);

var audit = new Audit();
audit.Created = DateTime.Now;
audit.User = CurrentUser.User; // Currently logged in user
audit.Type = "Post.Add";
audit.EntityId = post.Id;
audit.Message = "New post was created";

dataContext.Audits.Add(audit);

dataContext.SaveChanges();

In this instance, an audit entity will be added but the “EntityId” property will be set to 0 (default value) and not to the identity of the created user post, i.e. the identity value (SCOPE_IDENTITY()/@@IDENTITY).

I’d like to keep both points in a single transaction, rather than split the items into two transactions, i.e. persist the User Post first, then persist the Audit second as there is a chance the Audit may fail.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T08:40:57+00:00Added an answer on May 24, 2026 at 8:40 am

    Override DbContext.SaveChanges() and look at all the ChangeTracker.Entries().Where(e=>e.State != EntityState.Unchanged).

    Here is an example from a R&D project of mine:

    public override int SaveChanges()
    {
        int recordsUpdated = 0;
    
        var journalEntries = new List<AuditJournal>();
    
        foreach (var entry in this.ChangeTracker.Entries<ITrackedEntity>().Where(e=>e.State != EntityState.Unchanged))
        {
            AuditJournal journal = new AuditJournal();
            journal.Id = Guid.NewGuid();
            journal.EntityId = entry.Entity.Id.Value;
            journal.EntityType = entry.Entity.EntityType;
            journal.ActionType = entry.State.ToString();
            journal.OccurredOn = DateTime.UtcNow;
            //journal.UserId = CURRENT USER
            //journal.PreviousEntityData = XML SERIALIZATION OF ORIGINAL ENTITTY
    
            journalEntries.Add(journal);
        }
    
        using (var scope = new TransactionScope())
        {
            recordsUpdated = base.SaveChanges();
    
            foreach (var journalEntry in journalEntries)
                this.AuditJournal.Add(journalEntry);
    
            base.SaveChanges(); //Save journal entries
    
            scope.Complete();
        }
    
        return recordsUpdated;
    }
    
    //Every entity that needs to be audited has to implement this interface
    public interface ITrackedEntity
    {
        string EntityType { get; }
        Guid? Id { get; set; }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi I'm wondering if anyone could shed some light on the following problem for
I was wondering if anyone could help me with a vb6 function that would
I was wondering if anyone could point me to a resource that gave an
I was wondering if anyone could help with this. Let' say I have a
was wondering if anyone could help me improve my code. Its just some basic
I was wondering if anyone could help me with this problem: I have to
Afternoon, Could anyone offer any advice on how to put together a program that
I was wondering if anyone could help me vectorize these for loops I have
Was wondering if anyone could help me on background threading on Android. I have
I was wondering if anyone could explain and offer a solution to this issue:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.