title: Using a generic method in EF to check if a record exists in data base
body: <p>I'm developing an Asp.Net MVC Application and trying to make a generic method to check if a specific record exists in DB or not by using passed entityId to this method. Something like below:</p>
  
  <pre><code>public bool CheckIfUserExistsByUserId(int userId)
  {
      return _userRepository.DbSet().Any(u =&gt; u.Id == userId);
  }
  </code></pre>
  
  <p>But this method only checks _userRepository and accepts an integer as entityId.</p>
  
  <p>My problem is where I want to put this generic method as a general method in my <strong><em>BaseService</em></strong> just like other general methods that I write below:</p>
  
  <pre><code>public class BaseService&lt;TModel&gt; : IBaseService&lt;TModel&gt; where TModel : class
  {
      private readonly IUnitOfWork _unitOfWork;
      private readonly IBaseRepository&lt;TModel&gt; _baseRepository; 
  
      public BaseService(IUnitOfWork unitOfWork, IBaseRepository&lt;TModel&gt; baseRepository)
      {
          _unitOfWork = unitOfWork;
          _baseRepository = baseRepository;
      }
  
      public BaseService(IUnitOfWork unitOfWork)
      {
          _unitOfWork = unitOfWork;
      }
  
      public void Add(TModel entity)
      {
          this._baseRepository.Add(entity);
      }
  
      public void Remove(TModel entity)
      {
          this._baseRepository.Remove(entity);
      }
  
      /// &lt;summary&gt;
      /// Remove All Provided Items At Once .
      /// &lt;/summary&gt;
      /// &lt;param name="itemsToRemove"&gt;&lt;/param&gt;
      public void RemoveRange(IEnumerable&lt;TModel&gt; itemsToRemove)
      {
          _baseRepository.RemoveRange(itemsToRemove);
      }
  
      public TModel FindById&lt;T&gt;(T key)
      {
          return _baseRepository.FindById(key);
      }
  
      public void Commite()
      {
          this._unitOfWork.Commite();
      }
  }
  </code></pre>
  
  <p>But in this case, I can't get the primary key field of the entity that passed to my method from Entity Framework. Also, I don't want to pass the primary key field of the entity to my method in everywhere I use the mentioned method.
  Is it possible to getting the primary key field of the entity in EF?</p>
  
  <p>Edit: According to request of the other users I added my <strong><em>IBaseRepository</em></strong> source code here:</p>
  
  <pre><code>public interface IBaseRepository&lt;TModel&gt; where TModel : class
  {
      /// &lt;summary&gt;
      /// Add New Entity .
      /// &lt;/summary&gt;
      /// &lt;param name="entity"&gt;&lt;/param&gt;
      void Add(TModel entity);
  
      /// &lt;summary&gt;
      /// Remove Item From Database .
      /// &lt;/summary&gt;
      /// &lt;param name="entity"&gt;&lt;/param&gt;
      void Remove(TModel entity);
  
      /// &lt;summary&gt;
      /// Remove All Provided Items At Once .
      /// &lt;/summary&gt;
      /// &lt;param name="itemsToRemove"&gt;&lt;/param&gt;
      void RemoveRange(IEnumerable&lt;TModel&gt; itemsToRemove);
  
      /// &lt;summary&gt;
      /// Get The Underlying Type's DbSet .
      /// &lt;/summary&gt;
      /// &lt;returns&gt;&lt;/returns&gt;
      DbSet&lt;TModel&gt; DbSet();
  
      /// &lt;summary&gt;
      /// Find Item By Id .
      /// &lt;/summary&gt;
      /// &lt;typeparam name="T"&gt;&lt;/typeparam&gt;
      /// &lt;param name="key"&gt;&lt;/param&gt;
      /// &lt;returns&gt;&lt;/returns&gt;
      TModel FindById&lt;T&gt;(T key);
  
  }
  </code></pre>