title: Remove the selected item in a ListView from a SQL database
body: <p>Im trying to create a method that removes my selected item(from a listview)  from the database table <code>mrydenEntities()</code>
  Database is setup with <code>EntityFramework</code> and Database First model.</p>
  
  <p>Im getting this error </p>
  
  <blockquote>
    <p>System.InvalidOperationException: Sequence contains no elements</p>
  </blockquote>
  
  <p>Which I am pretty certain means that my code <code>recipeToRemove = context.Recipe.First(r =&gt; r.Id.Equals(Id));</code> is not getting any equals match.</p>
  
  <p>I am not sure what Im doing wrong, and Im running out on options how to move forward.</p>
  
  <p>My Db class:</p>
  
  <pre><code>public class Dbconnection : INotifyPropertyChanged
      {
  
      public event PropertyChangedEventHandler PropertyChanged;
  
      protected void OnPropertyChanged(string propertyName)
      {
          PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
      }
  
          public string RecipeName { get; set; }
          public string RecipeIngredients { get; set; }
          public string CookingTime { get; set; }
          public int Id { get; set; }
  
          mrydendbEntities context = new mrydendbEntities();
          public Recipe recipeToRemove;
          public Recipe Table { get; set; }         
  
          public void DbconnectionRemove(string RecipeName, string Ingredients, string CookingTime, int Id)
          {
              recipeToRemove = context.Recipe.First(r =&gt; r.Id.Equals(Id)); //This is giving me an error System.InvalidOperationException: Sequence contains no elements
              context.Recipe.Remove(recipeToRemove);
              context.SaveChanges();
              MessageBox.Show(recipeToRemove.RecipeName);
          }
      }
  </code></pre>
  
  <p>My Code Behind:</p>
  
  <pre><code>public partial class ViewAll : Page, INotifyPropertyChanged
      {
          public int Id { get; set; }
  
          public string RecipeName { get; set; }
  
          public string Ingredients { get; set; }
  
          public string CookingTime { get; set; }
  
          private Recipe _recipe;
          public Recipe Recipe
          {
              get { return _recipe; }
              set
              {
                  if(_recipe != value)
                  {
                      _recipe = value;
                      OnPropertyChanged("Recipe");
                  }
              }
          }
  
          public ViewAll()
          {
              InitializeComponent();
              LoadItemTemplate();
          }
  
          public void LoadItemTemplate()
          {
              mrydendbEntities dbe = new mrydendbEntities();
              listOfRecipes.ItemsSource = dbe.Recipe.ToList();
          }
  
          public event PropertyChangedEventHandler PropertyChanged;
          private void OnPropertyChanged([CallerMemberName] string propertyName = null)
          {
              PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
          }
  
          private void Button_Click(object sender, RoutedEventArgs e) //My button event to remove the selected table row.
          {
              var remove = new Dbconnection();
              remove.DbconnectionRemove(RecipeName, Ingredients, CookingTime, Id);
          }
  </code></pre>
  
  <p>My mrydendbEntities class:</p>
  
  <pre><code>using System;
      using System.Data.Entity;
      using System.Data.Entity.Infrastructure;
  
      public partial class mrydendbEntities : DbContext
      {
          public mrydendbEntities()
              : base("name=mrydendbEntities")
          {
          }
  
          protected override void OnModelCreating(DbModelBuilder modelBuilder)
          {
              throw new UnintentionalCodeFirstException();
          }
  
          public virtual DbSet&lt;Recipe&gt; Recipe { get; set; }
      }
  </code></pre>
  
  <p>EDIT: Changed <code>recipeToRemove = context.Recipe.First(r =&gt; r.Id.Equals(Id));</code> to be replaced with <code>recipeToRemove = context.Recipe.FirstOrDefault(r =&gt; r.RecipeName.Equals(RecipeName));</code> which gave me a new error message at the row <code>context.Recipe.Remove(recipeToRemove);</code></p>
  
  <blockquote>
    <p>System.ArgumentNullException "Value cannot be null"</p>
  </blockquote>