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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T16:29:10+00:00 2026-06-01T16:29:10+00:00

I have a couple models associated with a DbContext . How can I override

  • 0

I have a couple models associated with a DbContext. How can I override SaveChanges to do something different based on the model being saved?

For example, let’s say I have two models, Document and Paragraph. How can I override SaveChanges so that a directory is created when a Document is added and that a file is created when a Paragraph is added.

Here is my attempt at doing this so far.

public int override SaveChanges()
{
    ChangeTracker.DetectChanges();

    var context = ((IObjectContextAdapter)this).ObjectContext;
    var stateEntries = context.ObjectStateManager.GetObjectStateEntries(
        EntityState.Added
        | EntityState.Modified
        | EntityState.Deleted
    ).ToList();

    foreach (var entry in stateEntries)
    {
        if (!entry.IsRelationship)
        {
            switch (entry.State)
            {
                case EntityState.Added:
                    break;

                case EntityState.Modified:
                    break;

                case EntityState.Deleted:
                    break;
            }
        }
    }

    return base.SaveChanges();
}
  • 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-01T16:29:11+00:00Added an answer on June 1, 2026 at 4:29 pm

    The entry has Entity property which holds instance of your processed entity so you can check instance’s type and theoretically do whatever logic you want to do.


    You should not use SaveChanges to do anything else than processing your entities for persistence to database – that breaks the separation of concerns. The context is adapter between your application logic and database – nothing more. Moreover whole this idea of integrating file system operations with database operations needs much more complex and careful handling to solve the situation when any operation fails. In your current scenario any failure in call to base.SaveChanges will result in orphan directories or files in your file system – you need to use transactional file system and run but database and file system operation in the same transaction or you must implement manual file system compensation.

    Another problem with your code is that you must process Document instances prior to Paragraph to make sure that directories are created before you want to insert files, etc. Simply this logic doesn’t belong to SaveChanges method and should not be called from SaveChanges method.

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

Sidebar

Related Questions

I am working on a .Net MVC3 application. I have a couple different models
I have a couple of simple models that are associated like so: MODELS class
I have a couple of models in a Rails app. Let's say it's Products
I have a couple of methods I'd like to share between models rather than
We have couple different web-apps that share the same db link. The hibernate layer
I have a model and I'd like to restrict access to objects based on
I have a couple of models which share a has_many associations, named scopes and
I have a couple of models that are composites of multiple objects. I basically
I have a couple of models in my system: User Reputation Post Reputation Response
I have a couple of different profiles. I want to associate permissions with these

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.