-2

I am trying to retrieve from my db where the user is one. I am able to update where user is one, but when I try to retrieve the value I get an:

Unhandled Exception: System.ArgumentNullException: Value cannot be null.

Here is my cs file

using System;
using ConsoleApplication.Models;
using System.Linq;
using System.Collections;


namespace ConsoleApplication
{
    public class Program
    {
        public static void Main()
        {
            using (var db = new YourContext())
            {
                //perform database interactions
                var TableContents = db.Persons;
                Person NewPerson = new Person
                {
                    Name = "Name",
                    Email = "email@example.com",
                    Password = "HashThisFirst",
                    Age = 24
                };
                db.Add(NewPerson);
                db.SaveChanges();

                Person RetrievedUser = db.Persons.SingleOrDefault(user => user.ID == 1);
                RetrievedUser.Name = "Aaron";
                db.SaveChanges();
                System.Console.WriteLine(RetrievedUser.Name);

                RetrievedUser = db.Persons.SingleOrDefault(user => user.ID == 3);
                db.Persons.Remove(RetrievedUser);

                db.SaveChanges();

                Person ReturnedValues = db.Persons.Where(x => x.ID == 1).FirstOrDefault(); //This is the line causing the the run time error
                System.Console.WriteLine(ReturnedValues.Name);


            }

        }
    }
}

Here is the full error I am getting

Unhandled Exception: System.ArgumentNullException: Value cannot be null. Parameter name: entity at Microsoft.EntityFrameworkCore.Utilities.Check.NotNull[T](T value, String parameterName) at Microsoft.EntityFrameworkCore.Internal.InternalDbSet`1.Remove(TEntity entity) at ConsoleApplication.Program.Main()

Aaron
  • 4,380
  • 19
  • 85
  • 141

1 Answers1

1

Based on the information you provided, .Remove is reporting null input. That means retrievedUser is null, thus there is no user in your DB with ID == 3.