Null reference on an already used property. Looks like a bug. Has anyone else seen similar?
I'm in the process of migrating some data into a new DB and new schema using a script in LinqPad v6.9.15 Premium. I'm getting a null reference exception I can't explain as the item in question has already been accessed. (See screenshot below. If ProductProperty was actually null, I would expect the exception to get thrown on the "Count" line, but it doesn't. The mouse over tool-tip also shows the expected type in the first case, "NullReferenceException" in the second.) The same code works successfully to migrate to an SQLite database initialized using the same EF Core data model, but fails when run against an Azure SQL instance. Any suggestions?
Comments
It looks as if it may be related to reserved words in the context. I commented out the specific table and the remainder of the script could run successfully apart from the one that was acting on a table named "LicenseActivationOption" which failed in an identical manner. The Count operation succeeded, but an InsertAllOnSubmit call also generated the same NullReferenceException.
If you have optimizations turned on, the line may not be correctly highlighted for an exception. If your productProperties doesn't instantiate the query (e.g. with ToList) then you won't see an error in the query until you use it in InsertAllOnSubmit. I would speculate the error is being caused by instantiating the productProperties query where a null value is trying to be loaded into a property in the IntermediateProductProperty class that doesn't allow nulls.
Take a look at the exception's StackTrace - the exception is thrown from the InsertOnSubmit method, which means the null reference is happening from within LINQ-to-SQL itself. This might be because you're trying to insert a table entity that's not properly annotated for LINQ-to-SQL, although it's hard to tell because I don't know where SQLiteConnection comes from.