I inherited a project where the data model is created based on the database using EF 6. I'm creating a viewmodel as follows, where Data is the type that is autogenerated for my database context (i.e. it contains the fields corresponding to the tables in the DB).
public class ViewModel : INotifyPropertyChanged
{
private Data _data = new Data();
private ObservableCollection<Order> _orders;
public ObservableCollection<Order> Orders
{
get { return _orders; }
set
{
_orders = value;
OnPropertyChanged("Orders");
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(String propertyName = null)
{
var handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
}
Binding the grid in the XAML code shouldn't be hard and only requires me to point to the property Orders. However, I don't see exactly how the property will be populated from the DB (I haven't seen any Load method) nor how the DB will be updated (if the grid value is changed and the binding set to TwoWay).
I've googled it but didn't find anything spot-on. Suggestions?
Data is autogenerated in the following class.
public partial class Data : DbContext
{
public Data() : base("name=Data") { }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
throw new UnintentionalCodeFirstException();
}
public virtual IObservable<Order> Orders { get; set; }
}
The problem is that when I create an instance of Data, the property Orders seem to be null and I'm not sure how to populate it. There's no methods for loading, enumerating, selecting, where'ing etc... It's of type IObservable, not IEumerable and I can't see any ToList or ToEnumerable methods neither...
Edit
As I try to load in different tables into the set, I notice that one of them is null for no apparent reason.