Skip to content

How to implement OnPropertyChanged

Simon Hughes edited this page Jul 12, 2023 · 4 revisions

In <database>.tt, approx. line 292 set the table base class:

Settings.UpdateTable = delegate(Table table)
{
    table.BaseClasses = " : INotifyPropertyChanged";
};

In <database>.tt, approx. line 109 set:

Settings.AdditionalNamespaces = new List<string> { "System.ComponentModel" , "System.Runtime.CompilerServices" };

In EF.Reverse.POCO.v3.ttinclude, approx. line 404 set:

public static Func<Table, string> WriteInsideClassBody = delegate (Table t)
{
    return "    public event PropertyChangedEventHandler PropertyChanged;" + Environment.NewLine +
           "    protected virtual void OnPropertyChanged([CallerMemberName] string name = null)" + Environment.NewLine +
           "    {" + Environment.NewLine +
           "        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));" + Environment.NewLine +
           "    }" + Environment.NewLine;
}

In EF.Reverse.POCO.v3.ttinclude, approx. line (ef6 = 18611, EfCore2 = 20105, EfCore3 = 21690, EfCore5 = 23323, EfCore6 = 24961, EfCore7 = 26606) Search for public {{#if OverrideModifier}}override {{/if}}{{WrapIfNullable}} to find the right place. Under that line add in:

    private {{WrapIfNullable}} _{{NameHumanCase}};{{#newline}}
    public {{#if OverrideModifier}}override {{/if}}{{WrapIfNullable}} {{NameHumanCase}} { get { return _{{NameHumanCase}}; } {{PrivateSetterForComputedColumns}}set { if(value != _{{NameHumanCase}}) { _{{NameHumanCase}} = value; OnPropertyChanged(); } } }{{PropertyInitialisers}}{{InlineComments}}{{#newline}}

Save EF.Reverse.POCO.v3.ttinclude, then save <database>.tt