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

  • Home
  • SEARCH
  • 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 8223941
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T14:53:13+00:00 2026-06-07T14:53:13+00:00

Problem I am implementing a UnitOfWork and a Repository style data access layer for

  • 0

Problem

I am implementing a UnitOfWork and a Repository style data access layer for my web application. I want to audit changes to the database using a custom table I have made. The problem occurs when a new item is added to the context. The item is added to the context, however SaveChanges() method has not been called so the entity is still in the Added state. This means that is has not been assigned a primary key, which means I can not create an audit record and calling SaveChanges() from the repository would violate the repository coding style. Is there a way to get the primary key of an entity in the added state?

Attempted Solution

The only solution that I can think of is to use random GUIDs so that the client can generate them. I am a little hesitant because I have heard that using random GUIDs as the identity field can be costly.

Code

//Method in my repository class
public void Insert(T entity)
{
    //Adds the entity the context
    dbSet.Add(entity);

    Audit audit= new Audit 
    {
        Created = DateTime.Now,
        PrimaryKey = entity.Key, //This is not set because the State of the entity is still Added
    };

    Context.Audits.Add(audit);
}
  • 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-06-07T14:53:18+00:00Added an answer on June 7, 2026 at 2:53 pm

    Simple answer is you can’t do this in a distributed environment. You can’t guarantee that the ID will be the same once the item is persisted.

    Using client generated GUIDs does solve the problem. This is the approach I normally take, though let’s not start a debate on that! You will mostly find the outcome is 50/50. Both have pros and cons. For your scenario, it works!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having a problem implementing a bi-directional parent/child relationship using hibernate 3. The parent,
I am implementing a small database(university Project) and i am facing the following problem.
I have a slight problem implementing vibration functionality in my application. My code is:
I have a problem in implementing Mashups in PHP. Now I am using PHP
I have a problem implementing dropdownchoice in my code. I want to display a
Hi I'm having a problem implementing this method. I have added using System.Management but
I have a problem with implementing database table library. I have a class Column
I have some problem implementing a segment control. Because i want it to be
I have a problem implementing this logic in my project. Im using ServiceLoader and
I have a problem with implementing Facebook posting on wall in my iPhone application.

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.