aneira genérica de verificar se a entidade existe no Entity Framewor

Igual aMelhor maneira de verificar se o objeto existe no Entity Framewor

Estou procurando uma maneira genérica de verificar uma entidade em umDbSet. Algo assim, que não funciona:

private DbContext DbContext { get; set; }

private DbSet<T> DbSet { get; set; }

public Boolean Exists(T entity) {
    return ((from item in this.DbSet
             where item == entity
             select item).Count() > 0);
}

A linhawhere item == entity funciona em LINQ to SQL, mas aparentemente não com LINQ to Entities. Como as entidades podem ter chaves diferentes, não posso herdar todas elas de um resumo comum com uma chave conhecida para comparação.

Eu poderia fazer isso, mas estou preocupado com o desempenho de capturar exceções como um processo de verificação Isso também não funciona, desde que a entidade esteja desanexada doOriginalValues propriedade não pode ser obtida:

public Boolean Exists(T entity) {
    try {
        var current = this.DbContext.Entry(entity).OriginalValues;
        // Won't reach this line if the entity isn't in the database yet
        return true;
    }
    catch (Exception ex) {
        return false;
    }
}

questionAnswers(2)

yourAnswerToTheQuestion