C# 使用LINQ到SQL实现INotifyPropertyChanged

C# 使用LINQ到SQL实现INotifyPropertyChanged,c#,windows-phone-7,windows-phone-8,windows-phone,inotifypropertychanged,C#,Windows Phone 7,Windows Phone 8,Windows Phone,Inotifypropertychanged,我正在为我的实体使用LINQ to SQL构建一个Windows Phone 8应用程序。我当前的实现使用简单的inotifyPropertyChanged/inotifyPropertyChanged方法: [Table] public class Item : INotifyPropertyChanged, INotifyPropertyChanging { private int _itemId; [Column(IsPrimaryKey = true, IsDbGene

我正在为我的实体使用LINQ to SQL构建一个Windows Phone 8应用程序。我当前的实现使用简单的inotifyPropertyChanged/inotifyPropertyChanged方法:

[Table]
public class Item : INotifyPropertyChanged, INotifyPropertyChanging
{
    private int _itemId;

    [Column(IsPrimaryKey = true, IsDbGenerated = true, DbType = "INT NOT NULL Identity", AutoSync = AutoSync.OnInsert)]
    public int ItemId
    {
        get { return _itemId; }
        set
        {
            if (_itemId != value)
            {
                NotifyPropertyChanging("ItemId");
                _itemId = value;
                NotifyPropertyChanged("ItemId");
            }
        }
    }

    [Column]
    internal int? _groupId;

    private EntityRef<Board> _group;

    [Association(Storage = "_group", ThisKey = "_groupId", OtherKey = "GroupId", IsForeignKey = true)]
    public Group Group
    {
        get { return _group.Entity; }
        set
        {
            NotifyPropertyChanging("Group");
            _group.Entity = value;

            if (value != null)
            {
                _groupId = value.BoardId;
            }

            NotifyPropertyChanging("Group");
        }
    }
}

这个问题有明确的解决办法吗?

我已经找到了解决办法。只需使用另一个实现重载该方法:

    protected T SetProperty<T>(ref T field, T value, [CallerMemberName] string propertyName = null)
    {
        if (EqualityComparer<T>.Default.Equals(field, value))
        {
            return value;
        }

        NotifyPropertyChanging(propertyName);
        field = value;
        NotifyPropertyChanged(propertyName);

        return value;
    }

    protected T SetProperty<T>(ref EntityRef<T> field, T value, [CallerMemberName] string propertyName = null) where T : class
    {
        NotifyPropertyChanging(propertyName);
        field.Entity = value;
        NotifyPropertyChanged(propertyName);

        return value;
    }
    public Group Group
    {
        get { return _group.Entity; }
        set
        {
            var group = _group.Entity;

            if (SetProperty(ref group, value))
            {
                _group.Entity = group;

                if (value != null)
                {
                    _groupId = value.GroupId;
                }
            }
        }
    }
    protected T SetProperty<T>(ref T field, T value, [CallerMemberName] string propertyName = null)
    {
        if (EqualityComparer<T>.Default.Equals(field, value))
        {
            return value;
        }

        NotifyPropertyChanging(propertyName);
        field = value;
        NotifyPropertyChanged(propertyName);

        return value;
    }

    protected T SetProperty<T>(ref EntityRef<T> field, T value, [CallerMemberName] string propertyName = null) where T : class
    {
        NotifyPropertyChanging(propertyName);
        field.Entity = value;
        NotifyPropertyChanged(propertyName);

        return value;
    }
    public Group Group
    {
        get { return _board.Entity; }
        set
        {
            if (SetProperty(ref _group, value) != null)
            {
                _groupId = value.GroupId;
            }
        }
    }