2

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()"

9
  • Your _context has either not been initialized or your Order 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. Commented Mar 24, 2017 at 21:14
  • 1
    @Mr.Brownstone I know there is data in the Table as stated in the SO question "I even have the ability from the SQL Server Object Explorer (in Visual Studio 2015 Community) to "View Data" that is in both tables." my question is what should I be drilling down into within _context during debug? to find where it stores the records.
    – Edward
    Commented Mar 24, 2017 at 21:24
  • 1
    @Mr.Brownstone its in the first paragraph of this post, also I added what I could from the _context drilldown at the bottom of this question, so that maybe it will show you something that has no meaning to me.
    – Edward
    Commented Mar 24, 2017 at 21:50
  • 1
    @Mr.Brownstone OMG you are a lifesaver, I was going to redo the who table, and it still wouldn't have fixed that. The issue was I have 3 DateTimes (Ordered,Filled,Delivered) and only Ordered is the only one set to NOT NULL. So this being the first time I every experienced this because from what I see you never have to declare nchar/nvarchar as string? nullable. I was unable to figure out from all the drill downs in DbSet where the actual query information was though. You want to make your comment an answer and I will accept it?
    – Edward
    Commented Mar 24, 2017 at 22:13
  • 1
    @Mr.Brownstone on a side note you can use the []() setup for links in comments as well, though it doesn't reduce characters used it makes for a cleaner read.
    – Edward
    Commented Mar 24, 2017 at 22:19

1 Answer 1

2

From looking at the stack trace you posted it seems as though the error is raised when Entity Framework tries to convert the results from the query to an instance of your model. Usually, the error you are encountering is raised when there is a problem with the model binding - in your case the ability of your properties to accept a null value. Ensure that your model properties match the table columns in terms of being able to accept a null value.

3
  • Well something broke from changing my DateTime's to nullable. While I can now query it will not update changes in the Edit (POST) controller action. I put a breakpoint inside this to see what is happening and it just tells me breakpoint will never be reached. In the browser all I get is page cannot be found. Which of course either comes from if (id != order.Id) or from catch (DbUpdateConcurrencyException){ if (!OrderExists(order.Id))
    – Edward
    Commented Mar 25, 2017 at 18:09
  • Besides this, since you are MCTS certified on the .NET platform, mind twisting your brain on this one in SO?
    – Edward
    Commented Mar 26, 2017 at 0:08
  • So from my first comment I decided to start from scratch and do a DB-first approach using Scaffold-DbContext from the package manager console. The only difference in the Order class was public virtual Company Company { get; set; } and in the Company class was public Company(){ Order = new HashSet<Order>(); } and public virtual ICollection<Order> Order { get; set; } which of these not being in my previous version is what disallowed me from updating the database?
    – Edward
    Commented Mar 26, 2017 at 1:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.