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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:06:30+00:00 2026-06-16T00:06:30+00:00

I’m currently working on a WPF MVVM application using MVVM Light as the MVVM

  • 0

I’m currently working on a WPF MVVM application using MVVM Light as the MVVM Framework, Entity Framework as the ORM, and MS Synch Framework as the means of synchronizing a Local Sql Compact DB with an online SQL database. The application is also fairly complex in scope, as it is meant to manage an asset, calculating wear and tear on the use of that asset through its lifetime. Thankfully I’m new to all these technologies so ignorance is bliss 🙂 I’ve found lots of tutorials and information on creating the Unit of Work Patter and Repository pattern. However, I’m using the new DbContext, which I’ve read already uses these two patterns.

My current issue relates to using the new DbContext in Entity Framework. I’ve used DbContext Generator template in VS, and so I have a MyDbModelContainer. I’ve used this directly from my view models, which creates a Context per VM, which I’m pretty sure is a bad idea. Here is my constructor for a Parent/Child type data entry scenario, I construct the container here, and them use MVVM Light to message over a selected item to the child VM.

  Private FatigueDbContext As FatigueModelContainer

Public Sub New()

    If IsInDesignMode = False Then

        FatigueDbContext = New FatigueModelContainer

        FatigueDbContext.CtMaterials.Load()
        CtMaterialsCollection = FatigueDbContext.CtMaterials.Local

        FatigueDbContext.CtManufactures.Load()
        CtManufactures = FatigueDbContext.CtManufactures.Local

    End If

End Sub

I want to keep the Context open for the lifetime of my View-Model so that I can use MyDbModelContainer.MyTable.Local for bindings. So while this is working, how should I handle this correctly?

My gut feeling is I need to somehow wrap the auto-generated MyDbModelContainer into some classes that basically only contain the tables and functions that I need to work with on that View-Model.

I’m not using a View-Model Locator, but rather another View-Model to manage my views, got the idea from Rachel’s blog , and I like the concept. However, it means that I’m creating all my View-Models up front, and initializing any of the view model dependencies up front. I only have one window, and am just switching between View-Models they stay in memory and I don’t have any way to close my DbContext when switching to a new View-Model.

Here is the code for the Shell View-Model

Public Class ShellViewModel
Inherits ViewModelBase

#Region "Fields"

Private _changePageCommand As ICommand
Private _currentPageViewModel As IPageViewModel
Private _pageViewModels As List(Of IPageViewModel)

#End Region

Public Sub New()
    Dim DialogService As New ModalDialogService

    ' Add available pages
    PageViewModels.Add(New ImportJobDataViewModel(DialogService))
    PageViewModels.Add(New TestViewModel())
    PageViewModels.Add(New ReverseBendUtilityViewModel(DialogService))

    ' Set starting page
    CurrentPageViewModel = PageViewModels(0)
End Sub

#Region "Properties / Commands"

Public ReadOnly Property ChangePageCommand() As ICommand
    Get
        If _changePageCommand Is Nothing Then

            _changePageCommand = New RelayCommand(Of IPageViewModel)(Sub(param) ChangeViewModel(param))

        End If
        Return _changePageCommand
    End Get
End Property

Private Function IsViewPageModel(viewPageModel As IPageViewModel) As Boolean
    If TypeOf (viewPageModel) Is IPageViewModel Then
        Return True
    Else
        Return False
    End If
End Function

Public ReadOnly Property PageViewModels() As List(Of IPageViewModel)
    Get
        If _pageViewModels Is Nothing Then
            _pageViewModels = New List(Of IPageViewModel)()
        End If
        Return _pageViewModels
    End Get
End Property

Public Property CurrentPageViewModel() As IPageViewModel
    Get
        Return _currentPageViewModel
    End Get
    Set(value As IPageViewModel)
        If _currentPageViewModel IsNot value Then
            _currentPageViewModel = value
            RaisePropertyChanged(Function() CurrentPageViewModel)
        End If
    End Set
End Property

#End Region

#Region "Methods"

Private Sub ChangeViewModel(viewModel As IPageViewModel)
    If Not PageViewModels.Contains(viewModel) Then
        PageViewModels.Add(viewModel)
    End If

    CurrentPageViewModel = PageViewModels.FirstOrDefault(Function(vm) vm Is viewModel)
End Sub

#End Region

End Class 

So to sum it all up… Should I be creating some separate class aside from the auto-generated FatigueModelContainer, what would that class look like, would it be just one more class, or would it be separate classes based on the business operations. Such as a class to Add, Update and Delete Manufactures, a class to Add, Update and Delete Materials, etc. Where should I be inserting it into the VM? Presumably in the Shell-View-Model?

  • 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-16T00:06:31+00:00Added an answer on June 16, 2026 at 12:06 am

    You can solve this problem without really touching your db context. The idea is that even if the VM stays in memory, you only need the model instance to be initialized when the VM’s view is active.

    There is a nice way of going about this in Caliburn.Micro: Screens and Conductors, but it’s really not specific to the mvvm framework.

    In the most basic case, as Rachel mentioned in the comment above, you extend your IPageViewModel to include functions which handle activation- and deactivation-logic for the VM: activation logic is called when the VM is activated, deactivation logic is called when the VM is deactivated.

    The actual call to activate/deactivate would be made in the container for the IPageViewModels, for instance:

    Public Property CurrentPageViewModel() As IPageViewModel
        Get
            Return _currentPageViewModel
        End Get
        Set(value As IPageViewModel)
            If _currentPageViewModel Is value Then
                Return
            End If
    
            If _currentPageViewModel IsNot Nothing Then
                _currentPageViewModel.Deactivate()
            End If
    
            _currentPageViewModel = value
    
            If value IsNot Nothing Then
                value.Activate()
            End If    
    
            RaisePropertyChanged(Function() CurrentPageViewModel)
        End Set
    End Property
    

    That way you can close the db connection/detach the DbContext when a page is deactivated and open it when the page is activated.

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

Sidebar

Related Questions

I am currently working on an application using WPF and MVVM. Now if I
Hy guys! I am currently working on a little WPF project using MVVM via
I'm currently working on a WPF application that uses MVVM. I've got a ListBox
I am developing an application that uses Entity Framework and WPF with MVVM design
I'm working on my first true WPF MVVM pattern application. Currently I have a
I am currently working on a WPF application with Caliburn framework. At the top
I'm using Visual Studio 2008. I'm currently working with WPF and I'm using Edit->Format
I'm currently working out the layout of a WPF Application and seem to have
I'm currently working on my first WPF application, and I'm curious as to whether
I am currently working on an application in WPF/C# for personal use. I am

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.