3

QUESTION

Main Question With a WPF application that is using a single dbContext across multiple windows; is a dbContext.Load() required for each window that creates a CollectionViewSource? If its not required, then how do you get around it?

If this question isn't clear, i've created a simple example in hopes to demonstrate. Below i've created a small simplified example.

I ask this because i am looking to reduce window loading times and i though this may be the case. If there is a local cache of some sort, by doing a load for every window am i causing a performance hit?

Follow On Question If it is not, is some sort of include required in the main window to load relational parts?

EXAMPLE

A WPF Application using a MainWindow, declares a dbContext "MyDbType"; which remains open while each NewSubWindow uses .ShowDialog() to open. Each new window is passed the dbContext. All windows declare a CollectionViewSource in their XAML.

The Db Definition

//The context
public class MyDbType : DbContext
{
    public DbSet<MyType1> MyTable1 { get; set; }
    public DbSet<MyType2> MyTable2 { get; set; }
}

// The main type
public class MyType1
{
    public int Id { get; set; }
    public string Tag { get; set; }
    public virtual ObservableCollection<MyType2> BunchOf2s { get; set; }
}

// A relational sub type
public class MyType2
{
    public int Id { get; set; }
    public string Name { get; set; }
    public virtual MyType1 MyParent { get; set; }
}

The WPF Main Window

public partial class MainWindow : Window
{
    public MyDbType db = new MyTypeDb();
    ....
    ...
    ...

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        myDb1ViewSource = ((CollectionViewSource)(this.FindResource("myDb1ViewSource")));
        db.MyTable1.Load();
        myDb1ViewSource.Source = db.MyTable1.Local;
    }

    public OpenNewWindow()
    {
        NewSubWindow newWin = new NewSubWindow(db);
        newWin.Owner = this;
        newWin.ShowDialog();
    }
}

A SubWindow

public partial class NewSubWindow : Window
{
    private MyDbType db;

    public NewSubWindow(MyDbType Db)
    {
        db = Db;
    }

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        myDb1ViewSource = ((CollectionViewSource)(this.FindResource("myDb1ViewSource")));
        db.MyTable1.Load(); // ----- DOES THIS SECOND WINDOW NEED THIS? OR IS IT JUST SLOWING THINGS DOWN?
        myDb1ViewSource.Source = db.MyTable1.Local;
    }
}
5
  • I'd just like to point out that DbContext is not a singleton and was not designed to be used as such... Commented Sep 17, 2013 at 22:09
  • @Stefan Denchev Can you please explain this further? Commented Sep 17, 2013 at 22:52
  • Using a single instance for prolonged amounts of time (or at all) can affect performance (hopefully nothing else, but i can't really comment on that)... There is caching so you shouldn't need to anyway. Commented Sep 17, 2013 at 22:55
  • @Stefan Denchev Ok thanks, i would have thought a single context would have less performance problems but i obviously wrong. Thanks Commented Sep 18, 2013 at 4:17
  • @UIlrvnd The caching is only during the lifetime of the context, so using a single instance should improve performance because what is loaded does not need to be reloaded. Commented Sep 13, 2018 at 7:45

1 Answer 1

1

Since you're always working with the same instance of MyDbType, there is no need to Load MyTable1 for each window.

Sign up to request clarification or add additional context in comments.

1 Comment

I find if you don't, then datagrids won't populate; which leads me to believe i must be doing something wrong perhaps? If i comment out "db.MyTable1.Load();" in the subwindow; datagrids won't populate.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.