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 8350717
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T08:24:24+00:00 2026-06-09T08:24:24+00:00

I want to implement Repository Pattern in my ASP.NET MVC + EF 4.3 project.

  • 0

I want to implement Repository Pattern in my ASP.NET MVC + EF 4.3 project.

Right now my DBContext class has an override to SaveChanges that receives a “userid” parameter in order to do Audit Trail.

Example:

  // This is overridden to prevent someone from calling SaveChanges without specifying the user making the change
public override int SaveChanges()
{
    throw new InvalidOperationException("User ID must be provided");
}

public int SaveChanges(int userId)
{
    // Get all Added/Deleted/Modified entities (not Unmodified or Detached)
    foreach (var ent in this.ChangeTracker.Entries().Where(p => p.State ==      System.Data.EntityState.Added || p.State == System.Data.EntityState.Deleted || p.State == System.Data.EntityState.Modified))
{
                    // For each changed record, get the audit record entries and add them
                    foreach (AuditLog x in GetAuditRecordsForChange(ent, userId))
                    {
                        this.AuditLogs.Add(x);
                    }
                }

                // Call the original SaveChanges(), which will save both the changes made and the audit records
                return base.SaveChanges();
            }

Now, my RepositoryBase class has something like this:

 public class RepositoryBase<C> : IDisposable
        where C : DbContext, new()
    {
        private C _DataContext;

        public virtual C DataContext
        {
            get
            {
                if (_DataContext == null)
                {
                    _DataContext = new C();
                    this.AllowSerialization = true;
                    //Disable ProxyCreationDisabled to prevent the "In order to serialize the parameter, add the type to the known types collection for the operation using ServiceKnownTypeAttribute" error
                }
                return _DataContext;
            }
        }

My question is how can I expose the SaveChanges(int) method inside my RepositoryBase class?

Any clue?

  • 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-09T08:24:25+00:00Added an answer on June 9, 2026 at 8:24 am

    You’re going to need to use your actual DbContext in your generic definition instead of DbContext base class. Then you’ll be able to call your overridden function

     public class RepositoryBase<C> : IDisposable
            where C : YourContextClassGoesHere, new()
    
    {
        private C _dataContext;
    
        public void SaveChanges()
        {
            int userId = GetUserId();
           _dataContext.SaveChanges(userId);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have implemented a repository pattern in my asp.net mvc web application... But i
I'm trying to understand the Repository Pattern , while developing an ASP.NET MVC application
In a Rob Conery-style ASP.NET MVC application, you typically have a repository: public class
We are currently using Spring MVC to implement REST web services. We now want
If I want to implement caching when I am using the repository pattern and
Is this a good guide if I want to implement the repository pattern in
what's bad to implement repository pattern without interfaces? Repository - class public class WebRepository<T>
given this class definition: public class Frame { IFrameStream CapturedFrom; } I want implement
Microsoft has announce that WindowsLiveID become a OpenID provider . I want implement it
I am a newbie to ASP.net & MVC 2, and have understood the basic

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.