实体框架 - 如何不运行CollectionChanged从数据库加载集合时?

时间:2015-02-19 12:14:57

标签: c# entity-framework

我有以下课程:

public class Orcamento
{
    public Orcamento()
    {
        Produtos = new ObservableCollection<Produto>();
        Produtos.CollectionChanged += ProdutosCollectionChanged;
    }

    public int Id { get; set; }

    public ObservableCollection<Produto> Produtos { get; private set; }

    private void ProdutosCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
    {
        //Some process here that should not be run if it is being loaded from the database
    }

}

当我在数据库中查找Budget类时,CollectionChanged事件会运行。请注意以下代码:

using (var contexto = new Contexto())
{

    Orcamento orcamento = contexto.Orcamentos.OrderByDescending(o => o.Id)
        .Include(x => x.Produtos)
        .FirstOrDefault();
    //More some code above...
}

问题:我如何知道用户何时修改了集合,或者何时通过加载数据库来修改集合,因为只希望在用户修改集合时触发事件?

1 个答案:

答案 0 :(得分:0)

在Orcamento类中添加Bool字段,当从数据库更改值时,将bool字段设置为true。检查ProdutosCollectionChanged事件中的bool值。