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;
}
}
Since you're always working with the same instance of MyDbType, there is no need to Load MyTable1 for each window.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With