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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T18:57:53+00:00 2026-05-22T18:57:53+00:00

In my WPF MVVM app, I’d like changes to my Entity Framework entity classes

  • 0

In my WPF MVVM app, I’d like changes to my Entity Framework entity classes automatically persisted back to the database. I’ve had a few ideas about implementing this functionality, but none of them seem optimal.

Here are my current (non-optimal) ideas:

  1. Use a DispatcherTimerto save changes every interval:

    DispatcherTimer timer = new DispatcherTimer();
    timer.Interval = TimeSpan.FromSeconds(1);
    timer.Tick += (sender, args) => Repository.SaveChanges();
    timer.Start();
    
  2. Save changes in the program’s OnExit() method.

    protected override void OnExit(ExitEventArgs e)
    {
        Repository.SaveChanges();
        base.OnExit(e);
    }
    
  3. Save changes every time OnPropertyChanged() is called on one of my Entities:

    protected virtual void OnPropertyChanged(string propertyName)
    {
        Repository.SaveChanges();
    }   
    
  4. A combination of 2 & 3; save the data after counting an arbitrary number of property changes and also save in OnExit() to catch any changes that didn’t reach the change limit:

    const int limit = 5;
    int changes = 0;    
    
    protected virtual void OnPropertyChanged(string propertyName)
    {
        if (++changes == limit)
        {
            Repository.SaveChanges();
            changes = 0;
        }
    }
    

I assume 1 & 3 are the worst performing out of all of them, and 2 (not 100% sure) won’t save any changes if the program crashes. 4 is my preferred option as it balances the amount of saves that are performed.

So, my question is: In your experience or otherwise, what’s the best way to approach/tackle this problem?

Thanks in advance.

  • 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-22T18:57:54+00:00Added an answer on May 22, 2026 at 6:57 pm

    To digress a little; do you also allow a manual way for the user to save? I believe this is important to the question, as it should help determine how stringent you need to be with your auto-save behaviour.

    Another consideration; are partial commits allowed? If a user changes one piece of information, the auto-save kicks in, the user changes another piece, and then the app crashes. Is it possible that auto-save could allow data corruption due to the fact that it has saved what was supposed to be an atomic operation that hadn’t fully completed?

    Now, since you mention that the database is a local one, you don’t have to worry too much about the latency of a client-server database. That means that a high frequency of commits should be fine. Therefore, I’d rule out option 2 completely. Saving once on exit seems too risky.

    I like the idea of reacting to property changes and saving then, but only if the partial commit consideration above isn’t going to be a problem. It will ensure all data is committed on a timely basis.

    If the user has a way of manually saving their data, I’d probably go with the timed approach. Set it to 10 seconds (or some value), and fire off the auto-save, with a notification displayed to the user that the auto-save has fired.

    The main thing to worry about, I believe, is what the user expects to happen. If you’re committing on every change, the user will (probably) not be surprised at the state of their data on a crash. Same goes with the auto-save notification. Informing the user of the save intervals will allow them to shrug off a crash, having confidence that the state of their data will still be relatively fresh.

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

Sidebar

Related Questions

I'm trying to implement a WPF application using MVVM (Model-View-ViewModel) pattern and I'd like
Considering you have an MVVM Architecture in WPF like Josh Smith's examples How would
I have begun creating a wpf mvvm app. It seems a vital ingredient to
For some project templates (ASP.NET MVC, WPF MVVM app...), Visual Studio 2008 prompts the
When writing a MVVM WPF app, there's always a point where the view model
In a WPF / EF4.0 / MVVM app I have a View that edits
I am creating a WPF app using MVVM. The app manages tagged documents, called
I've got a MVVM WPF app with the TreeView databound to a viewmodel class.
WPF, Browserlike app. I got one page containing a ListView. After calling a PageFunction
Some WPF controls (like the Button ) seem to happily consume all the available

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.