Context.SaveChanges() Throws SqlException: Invalid object name

0

I'm creating an admin portal for my application that uses Entity Framework to access the SQL Server database. I cannot figure out why this exception is happening, as the method that is triggering this has nothing to do with the object in which is shown in the exception. The problem is in my AuthorizeStaff method. It's executed fine but the exception triggers when I attempt to save my changes to the database. Here is my code:

public async Task<IActionResult> AuthorizeStaff(int staffId, int schoolId)
{
        var school = _context.Schools.SingleOrDefault(x => x.Id == schoolId);
        var requested = JsonConvert.DeserializeObject<List<Staff>>(school.RequestedStaff);

        var staff = requested.Single(x => x.Id == staffId);

        List<Staff> staffList = school.StaffList;
        staffList.Add(staff);
        school.Staff = JsonConvert.SerializeObject(staffList);

        requested.Remove(staff);
        school.RequestedStaff = JsonConvert.SerializeObject(requested);

        await _context.SaveChangesAsync();
        return Content("The staff member was authorized succesfully");
}

And here is my DbContext class:

public class HallPassContext : DbContext
{
    public HallPassContext(DbContextOptions<HallPassContext> options) : base(options) { }

    public DbSet<School> Schools { get; set; }

    public override int SaveChanges()
    {
        return base.SaveChanges();
    }
}

I'm completely stumped because I'm not incredibly experienced with Entity Framework and none of my code above involves the object "Pass."

Here's the error:

System.Data.SqlClient.SqlException (0x80131904): Invalid object name 'Pass'.

at System.Data.SqlClient.SqlCommand.<>c.b__122_0(Task1 result)
at System.Threading.Tasks.ContinuationResultTaskFromResultTask
2.InnerInvoke()
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
End of stack trace from previous location where exception was thrown

at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot)

End of stack trace from previous location where exception was thrown --- at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.ExecuteAsync(IRelationalConnection connection, DbCommandMethod executeMethod, IReadOnlyDictionary`2 parameterValues, CancellationToken cancellationToken) at Microsoft.EntityFrameworkCore.Update.ReaderModificationCommandBatch.ExecuteAsync(IRelationalConnection connection, CancellationToken cancellationToken) ClientConnectionId:61782dfe-2603-4360-bb7e-50b2c475910b Error Number:208,State:1,Class:16

Any help would be great, thanks!

.net
sql-server
asp.net-mvc
entity-framework
asked on Stack Overflow Sep 29, 2018 by AcePilot10 • edited Sep 29, 2018 by marc_s

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0