Linq 不会在添加新项时更新相关项

本文关键字:更新 新项时 添加 Linq | 更新日期: 2023-09-27 18:35:27

我正在构建一个与纸牌组和玩游戏有关的Windows Phone应用程序。我完全基于此示例"如何使用 MVVM for Windows Phone 8 创建本地数据库应用程序"。很明显,一个甲板可以玩几个游戏。因此,我构建了这样一种方式,即 Deck 是示例中的"类别",游戏是"待办事项"。附上模型和视图模型的具体代码。

当我添加一个新游戏时,它会得到一个它所属的套牌。这工作正常。

问题是 Deck 表中的 DeckGame 集合(显然)没有注意到它收到了新成员,因此当返回包含所有套牌信息的页面时,新游戏不存在。

简而言之,游戏知道它属于哪个套牌,但套牌不知道它有新游戏。

当我重新启动应用程序时,所有新游戏都存在。

这是我的代码中我认为相关的部分。

这是添加新游戏方法 (ViewModel),在注册新游戏时调用该方法。

    public void AddGame(GameItem newGameItem)
    {
        // Add a to-do item to the data context.
        _Db.Games.InsertOnSubmit(newGameItem);
        // Save changes to the database.
        _Db.SubmitChanges();
        // Add a to-do item to the "all" observable collection.
        Games.Add(newGameItem);
    }

这是套牌游戏关系中的游戏部分

// Internal column for the associated Deck ID value
    [Column]
    internal int _gameDeckId;
    // Entity reference, to identify the Deck "storage" table
    private EntityRef<DeckItem> _gameDeck;
    // Association, to describe the relationship between this key and that "storage" table
    [Association(Storage = "_gameDeck", ThisKey = "_gameDeckId", OtherKey = "DeckId", IsForeignKey = true)]
    public DeckItem GameDeck
    {
        get { return _gameDeck.Entity; }
        set
        {
            NotifyPropertyChanging("GameDeck");
            _gameDeck.Entity = value;
            if (value != null)
            {
                _gameDeckId = value.DeckId;
            }
            NotifyPropertyChanging("GameDeck");
        }
    }

这是关系的甲板部分

// Define the entity set for the collection side of the relationship.
    private EntitySet<GameItem> _deckGames;
    [Association(Storage = "_deckGames", OtherKey = "_gameDeckId", ThisKey = "DeckId")]
    public EntitySet<GameItem> DeckGames
    {
        get { return this._deckGames; }
        set { this._deckGames.Assign(value); }
    }

    // Assign handlers for the add and remove operations, respectively.
    public DeckItem()
    {
        _deckGames = new EntitySet<GameItem>(
            new Action<GameItem>(this.attach_Game), 
            new Action<GameItem>(this.detach_Game)
            );
    }
    // Called during an add operation
    private void attach_Game(GameItem game)
    {
        NotifyPropertyChanging("GameItem");
        game.GameDeck = this;
    }
    // Called during a remove operation
    private void detach_Game(GameItem game)
    {
        NotifyPropertyChanging("GameItem");
        game.GameDeck = null;
    }

任何帮助表示赞赏,不要犹豫,询问更多详细信息或代码!

已编辑 2015-01-01

    // All Game items.
    private ObservableCollection<GameItem> _games;
    public ObservableCollection<GameItem> Games
    {
        get { return _games; }
        set
        {
            _games = value;
            NotifyPropertyChanged("Games");
        }
    }

Linq 不会在添加新项时更新相关项

我想您的代码中存在错误(而且,可悲但确实如此,在MSDN示例中)。

看看这个片段:

public DeckItem GameDeck
{
    get { return _gameDeck.Entity; }
    set
    {
        NotifyPropertyChanging("GameDeck");
        _gameDeck.Entity = value;
        if (value != null)
        {
            _gameDeckId = value.DeckId;
        }
        NotifyPropertyChanging("GameDeck");
    }
}

您两次通知 GameDeck 属性即将更改,但从未通知它已更改

第二个NotifyPropertyChanging方法调用应替换为NotifyPropertyChanged方法调用。