【问题标题】:ObservableCollection edits are not savingObservableCollection 编辑未保存
【发布时间】:2014-02-20 15:06:44
【问题描述】:

我的 ObservableCollection 终于提取了数据,但现在它没有保存编辑。

这是我的代码:

public void FillDataGrid(Guid corporationId)
{
    var query = from s in entity.Sources
                where s.CorporationId == corporationId
                select new SourceItem
                {
                    CorporationId = s.CorporationId,
                    Description = s.Description,
                    IsActive = s.IsActive,
                    Name = s.Name,
                    SourceId = s.SourceId,
                    TokenId = s.TokenId
                };

    SoureItemCollection = new ObservableCollection<SourceItem>(query);
    SourceDataGrid.ItemsSource = SoureItemCollection;
    SourceDataGrid.Columns[2].Visibility = Visibility.Hidden;
    SourceDataGrid.Columns[0].IsReadOnly = true;
    SourceDataGrid.Columns[1].IsReadOnly = true;
}

这是我要绑定的类:

public class SourceItem
{
    private Guid _corporationId1;
    private string _description;
    private bool _isActive;
    private string _name;
    private Guid _sourceId;
    private Guid _tokenId;

    public Guid CorporationId
    {
        set
        {
            _corporationId1 = value;
            onPropertyChanged(this, "CorporationId");
        }
        get { return _corporationId1; }
    }

    public string Description
    {
        set
        {
            _description = value;
            onPropertyChanged(this, "Description");
        }
        get { return _description; }
    }

    public bool IsActive
    {
        set
        {
            _isActive = value;
            onPropertyChanged(this, "IsActive");
        }
        get { return _isActive; }
    }

    public string Name
    {
        set
        {
            _name = value;
            onPropertyChanged(this, "NAme");
        }
        get { return _name; }
    }

    public Guid SourceId
    {
        set
        {
            _sourceId = value;
            onPropertyChanged(this, "SourceId");
        }
        get { return _sourceId; }
    }

    public Guid TokenId
    {
        set
        {
            _tokenId = value;
            onPropertyChanged(this, "TokenId");
        }
        get { return _tokenId; }
    }

    // Declare the PropertyChanged event
    public event PropertyChangedEventHandler PropertyChanged;

    // OnPropertyChanged will raise the PropertyChanged event passing the
    // source property that is being updated.
    private void onPropertyChanged(object sender, string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(sender, new PropertyChangedEventArgs(propertyName));
        }
    }
}

还有保存:

private void Save_Click(object sender, RoutedEventArgs e)
{
    entity.SaveChanges();
}

我想在保存之前将 ObservableCollection 绑定回实体吗?如果是这样,我该怎么做?

【问题讨论】:

    标签: c# linq entity-framework observablecollection


    【解决方案1】:

    您已创建数据的副本并正在编辑该副本。实体框架上下文完全不知道这一点。

    您可以使用实体数据对象,而不是使用SourceItem 中的字段。例如:

    var query = from s in entity.Sources
                where s.CorporationId == corporationId
                select new SourceItem(s);
    
    class SourceItem
    {
      private SourceEntity _model;
    
      public SourceItem(SourceEntity model)
      {
          _model = model;
      }
    
      public Guid CorporationId
      {
        get { return _mode.CorporationId; }
        set
        {
          _model.CorporationId = value;
          OnPropertyChanged(this, "CorporationId");
        }
      }
    }
    

    请注意,这与ObservableCollection 无关。如果您要从集合中添加或删除项目,您还需要将其同步到实体模型。您可以为此使用CollectionChanged 事件。

    【讨论】:

    • 我的绑定仅用于保存已经存在的记录。 ObservableCollection 的这一点是为了让添加和删除工作......
    • 我正在尝试实现这一点,但我遇到了SourceItem 的问题。我不确定那到底是什么? _model 未扩展到您所显示的基础字段。
    【解决方案2】:

    看到问题在于,使用表达式“var query = from s in entity.Sources .. select new SourceItem{..}”您没有检索到附加到上下文的实体(实体) 但您创建了 SourceItem 类型的新对象,这些对象无法映射回 db 实体。 你应该做的是你应该改变你的 SourceItem 类,它直接由 entity.Sources 对象支持(我猜它应该是它的构造函数参数)。将 SourceItem 字段委托回相应的 Sources 字段(两种方式)。所以 SourceItem 将封装 Sources 对象并用所有 WPF 东西装饰它以进行 MVVM 操作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多