是的,我已经这样做了,它成功地适用于我的业务应用程序。我将 Model.tt 文件修改为具有 virtualObservableCollection<T>
而不是ICollection<T>
并将其替换HashSet<T>
为相同的。
我还INotifyPropertyChanged
通过以下实现在实体上实现:
public event PropertyChangedEventHandler PropertyChanged;
protected void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
{
var handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
我需要包含三个额外的 using 语句:
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Collections.ObjectModel;
这是我在 CodeStringGenerator 中更改以实现我的 getter 和 setter 的函数:(对不起,我还没有来使这个更具可读性)
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)));
}
这是一个完整生成的实体文件示例供参考:
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; }
}
}