So I'm pretty far down the rabbit hole of using the Entity Framework designer to make an EDMX that serves as the model in an MVVM project.
I've just come across an issue where I'm pretty sure that the ICollection<>
that was code generated (see below for example) really needs to be an ObservableCollection<>
for binding that collection to a DataGrid
in a view to be successful.
I think I'm getting some hits on the possibility of modifying the EF code generation to make ObservableCollections
rather than ICollections
. Any one ever tried that successfully?
I suppose another option would be have the VM that contains the selected Customer object also contain a local ObservableCollection<Order>
that gets created when the Customer object is selected....I just worry about the context saves and keeping the data in sync.
typical code gen object with an association to a collection of child objects :
public partial class Customer
{
public Customer()
{
this.Orders = new HashSet<Order>();
}
public int Id { get; set; }
public System.DateTime Date { get; set; }
public virtual ICollection<Order> Orders { get; set; }
}
Your
data logic
andmodels
should be separate from yourviewmodel
andmodels
. So, I would think that the better option is what you are talking about in creating anObservableCollection
. You can always sync to the context (I forget the exact syntax to sync) when you save.The
DbSet
class that is normally exposed via yourDbContext
has aLocal
property which is anObservableCollection<T>
. See the official documentation for more informationThat's what I did and what works for me with EF Database first.
That's what you need to be generated:
So that default costructor will be replaced. And ObservableCollection is ICollection, so you don't need to change anything else.
To make this appear every time you update your database model you have to change your .tt file with following parts:
and this:
Yes, I have done this and it works successfully for my business application. I modified the Model.tt file to have virtual
ObservableCollection<T>
instead ofICollection<T>
and replaced theHashSet<T>
with the same.I also implemented
INotifyPropertyChanged
on the entities with the following implementation:I needed to include three extra using statements:
This is the function that I changed in the CodeStringGenerator to implement my getters and setters: (Sorry, I still haven't come around to making this more readable)
And this is a sample full generated entity file for reference: