1

I have a program, which uses MS SQL database. I decided to switch to MVVM WPF pattern and use Entity Framework for database stuff and Catel for MVVM stuff.

I created database first model and long-term context in my code (Global.Database). I changed ICollection and HashSet types in my model's *.tt file to ObservableCollection.

Now i have WPF View and View Model. At first i decided to show some basic database connection info on my view.

So, i created a model:

 public class DbInfo :ModelBase
{
    public DbInfo()
    {

        ServerName = Global.Database.Database.Connection.DataSource;
        DbName = Global.Database.Database.Connection.Database;
        RegisteredClientsCount = Global.Database.Clients.Count() - 1;
    }
    public string ServerName
    {
        get { return GetValue<string>(ServerNameProperty); }
        set { SetValue(ServerNameProperty, value); }
    }


    public static readonly PropertyData ServerNameProperty = RegisterProperty("ServerName", typeof(string), string.Empty);

    public string DbName
    {
        get { return GetValue<string>(DbNameProperty); }
        set { SetValue(DbNameProperty, value); }
    }

    public static readonly PropertyData DbNameProperty = RegisterProperty("DbName", typeof(string), string.Empty);

    public int RegisteredClientsCount
    {
        get { return GetValue<int>(RegisteredClientsCountProperty); }
        set { SetValue(RegisteredClientsCountProperty, value); }
    }

    public static readonly PropertyData RegisteredClientsCountProperty = RegisterProperty("RegisteredClientsCount", typeof(int), 0);

}

It derives from ModelBase class, so, i guess, it has to be ObservableObject.

Then i created a viewmodel with just one property:

public class MainWindowViewModel : ViewModelBase
{
    public MainWindowViewModel()
    {
        DatabaseInfo = new DbInfo();

    }

    public DbInfo DatabaseInfo
    {
        get { return GetValue<DbInfo>(DatabaseInfoProperty); }
        set { SetValue(DatabaseInfoProperty, value); }
    }

    public static readonly PropertyData DatabaseInfoProperty = RegisterProperty("DatabaseInfo", typeof(DbInfo), null);
  }

Then i created a label in my View and added a context binding:

Context="{Binding DatabaseInfo.RegisteredClientsCount}"

All i want is to find out, how the hell i can now track the changes in database? When i run my app, this label shows the proper value - the quantity of "clients" table rows. Then i add some rows manually into database - nothing happens in my window.

Maybe i have some missunderstanging with all of this stuff, but i expected my database data binded to wpf controls and wrapped into all these PropertyChanged interfaces and stuff being updated on database change. Am i wrong? What should I do? Thanks a lot for help.

rovnyart_
  • 161
  • 11
  • This question is too complex. You are showing just the model and view model, but there is no "tracking of db" at all in this piece of code. I suggest you first try to get the parts up and running separately (you already have MVVM working I see), so search SO for DB tracking using EF instead. – Geert van Horrik Feb 13 '17 at 16:27
  • Ok, this is almost all of my code actually=) The only thing i do in App.xaml.cs is creating an instance of my Global.Database (= new dbContext()). My problem is that i can't understand, do I need anything more to make thinks work or not? I try to use MVVM pattern, so i can't use any "timers" or other event handlers, where i have to manually update my context. I assumed, that if i have all of these "preperty changes notifiers" and "observable collections", i don't need anything else. As I can see, i was wrong – rovnyart_ Feb 13 '17 at 16:34
  • In regular winforms app all i needed to do is to dispose my context and create it again in some sort of separate thread or by some event, but here - i just cant' understand WHERE to put my code of this "refreshing". everything seems to bee "auto-refreshing" for me – rovnyart_ Feb 13 '17 at 16:43

0 Answers0