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 of ICollection<T>
and replaced the HashSet<T>
with the same.
I also implemented INotifyPropertyChanged
on the entities with the following implementation:
public event PropertyChangedEventHandler PropertyChanged;
protected void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
{
var handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
I needed to include three extra using statements:
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Collections.ObjectModel;
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)
public string Property(EdmProperty edmProperty)
{
var fourSpaces = " ";
return string.Format(
CultureInfo.InvariantCulture,
"{0} {1} _{2};{3}{4}{0} {1} {2}{3}{4}{{{3}{4}{4}{5}get {{ return _{2}; }} {3}{4}{4}{6}set{3}{4}{4}{{{3}{4}{4}{4}if (value == _{2}) return;{3}{4}{4}{4}_{2} = value;{3}{4}{4}{4}NotifyPropertyChanged();{3}{4}{4}}}{3}{4}}}{3}",
Accessibility.ForProperty(edmProperty),
_typeMapper.GetTypeName(edmProperty.TypeUsage),
_code.Escape(edmProperty),
Environment.NewLine,
fourSpaces,
_code.SpaceAfter(Accessibility.ForGetter(edmProperty)),
_code.SpaceAfter(Accessibility.ForSetter(edmProperty)));
}
And this is a sample full generated entity file for reference:
namespace Eagl.Eagle.Data
{
using System;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Collections.Generic;
using System.Collections.ObjectModel;
public partial class Game : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
{
var handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
public Game()
{
this.Playtests = new ObservableCollection<Playtest>();
}
public int _Id;
public int Id
{
get { return _Id; }
set
{
if (value == _Id) return;
_Id = value;
NotifyPropertyChanged();
}
}
public string _Name;
public string Name
{
get { return _Name; }
set
{
if (value == _Name) return;
_Name = value;
NotifyPropertyChanged();
}
}
public virtual ObservableCollection<Playtest> Playtests { get; set; }
}
}