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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T15:56:07+00:00 2026-06-05T15:56:07+00:00

I am building an ASP.Net MVC 3 Web application with Entity Framework 4.1. I

  • 0

I am building an ASP.Net MVC 3 Web application with Entity Framework 4.1. I am using the Database First approach.

I have also used the ADO.NET DbContext Generator in order to create POCO classes as opposed to using the auto generated Entity Objects. When you use the ADO.NET DbContext Generator it will create two new items, a .tt file which generates the POCO classes for each Entity, and also a .Context.tt file which generates a derived DbContext class (used for querying and persisting data).

In my application I have written code to perform Auditing for particular Entities, this code executes inside the override SaveChanges() method I have created.
I have placed this override SaveChanges() method inside the Context.cs class as follows and it works nicely

public partial class LocumEntities : DbContext
{
    public LocumEntities()
        : base("name=LocumEntities")
    {
    }

    public override int SaveChanges()
    {
        //Audit Code Executes in Here
        return base.SaveChanges();
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        throw new UnintentionalCodeFirstException();
    }

    public DbSet<Audit> Audits { get; set; }
    public DbSet<Form> Forms { get; set; }
    //Other DbSets
}

However, anytime I even move or update an Entity in my EDMX Diagram, and then save, my custom SaveChanges() method inside the Context.cs class is deleted.
Is there any way I can stop this from happening, or maybe I should be putting my SaveChanges() method elsewhere?

Can someone please advise?

Thanks.

  • 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-05T15:56:10+00:00Added an answer on June 5, 2026 at 3:56 pm

    Context.cs is not a class, it’s a file, with one (or zero, or many) classes inside.

    Just add a new cs File, for example CustomContext.cs,

    and add

    public partial class LocumEntities : DbContext {
    
        public override int SaveChanges()
        {
            //Audit Code Executes in Here
            return base.SaveChanges();
        }
    
        //other custom code (not generated by T4)
    }
    

    as it’s partial, you can have part of LocumEntities class in many .cs files if you want, they will be considered as one class. Just be sure they’re in the same namespace.

    Never write your own code in a generated file…

    Generated classes are generally partial just to enable that way to handle custom code.

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

Sidebar

Related Questions

I am building an ASP.NET MVC web application using entity framework DbContext using the
I am building an ASP.Net MVC 3 web application using Entity Framework 4.1 with
I am building an ASP.Net MVC 3 Web application using Entity Framework 4.1. To
I'm in the process of building my first web application using ASP.NET MVC 2
I'm building a web app using EF Code First and ASP.NET MVC. I have
I'm am building my asp.net web application using MVC (Preview 5), and am also
I am building a web application using ASP.NET MVC that has two very distinct
I am building an ASP.NET MVC web application and I'm using jQuery for some
I've started building a ASP net MVC 2.0 web application using SQL Express 2008
I'm building a small web application with ASP.NET MVC 2, using db4o as a

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.