I am experiencing an issue, which is beyond my understanding in trying to figure out, I am not sure if it is some glitch with Visual Studio, or something wrong with my SQL Server DB. I originally thought I just broke my code in an WebAPI I was creating, hence my question on SO. But on further evaluation it could be something more.
The issue is even occurs with creating a new ASP.Net Core WebAPI with Individual Authentication from the template, only adding two classes, and scaffolding out the controllers and views, I am only able to query one table while being able to save to all tables. This with the fact my Model class data is from one database and the AspNetUser tables are on another database while all on the same SQL Server server.
For reference anytime Index/Detail/Edit is called to controllers for my Order model class it gives a "NullReferenceException: Object reference not set to an instance of an object." in the browser from
await _context.Order.ToListAsync());
And yet I can type /Order/Create to get to the Create view for Order and have it update the DB. While my Company model class allows for lookups, updates, and inserts to the DB; both of which run off the same DBContext connection.
This issue appeared after another issue I had and put on hold to figure out, and that other issue I noticed was when I "registered" a new user everything worked fine but when I would logoff and try to log back in it always failed. I thought maybe it had to do with an incorrect size on the nvarchar on a field; but I have lookup the newest setups and seem to be fine there, as I was thinking I was maybe truncating some value. The one reason I didn't correlate these two issues together is because the first one with User login didn't throw any error code in the browser (but of course Identity.EntityFramework probably already has some behind the scene error checks) to take care of that. But since the User cannot log back in after a register and logoff, that is telling me that a query is failing even though its from a separate connection. And this fails no matter how many times I try to create a new program.
Update: I changed my connectionString to include MARS
"Data Source={ServerName}\{InstanceName}; Initial Catalog={DatabaseName}; Integrated Security=True; Connect Timeout=15; MultipleActiveResultSets=true;"
for both my userLogin database and my appDatabase. This allowed me to log back in for the user. It did not however let me query the Order table. I therefore tried a new class, State (which as Name/Code of all US states), I then scaffolded out the controller and views. The query works now for both Company and State but not Order. So I thought it had to do with the virtual keyword in the DataMineDbContext for the DBSets, as State did not scaffold that in. But that did not fix my issue. I therefore though it was a naming convention thing and renamed Order to Purchase both in the app and in my Database. This has not helped.
From comment by Mr. BrownStone the information obtained from _context in the Orders (now Purchases) Controller
HResult: -2147467261
InsideExpression: _COMPlusExceptionCode: -532462766 s_EDILock: {object}
Messge: "Object reference not set to an instance of an object."
Source: "Anonymously Hosted DynamicMethods Assembly"
StackTrace: "at lambda_method(Closure , ValueBuffer )\r\n at Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.Internal.UnbufferedEntityShaper`1.Shape(QueryContext queryContext, ValueBuffer valueBuffer)\r\n at Microsoft.EntityFrameworkCore.Query.QueryMethodProvider._ShapedQuery>d__`1.MoveNext()\r\n at Microsoft.EntityFrameworkCore.Query.Internal.LinqOperatorProvider._TrackEntities>d__15`2.MoveNext()\r\n at Microsoft.EntityFrameworkCore.Query.Internal.LinqOperatorProvider.ExceptionInterceptor`1.EnumeratorExceptionInterceptor.MoveNext()\r\n at System.Collections.Generic.EnumerableHelpers.ToArray[T](IEnumerable`1 source, Int32& length)\r\n at System.Collections.Generic.EnumerableHelpers.ToArray[T](IEnumerable`1 source)\r\n at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)\r\n at System.Linq.SystemCore_EnumerableDebugView`1.get_Items()"
_context
has either not been initialized or yourOrder
table has no records in it. Put a breakpoint on the line that throws an error and hover over_context
to see if it is null and check to see if the table has rows.