This thread looks to be a little on the old side and therefore may no longer be relevant. Please see if there is a newer thread on the subject and ensure you're using the most recent build of any software if your question regards a particular product.
This thread has been locked and is no longer accepting new posts, if you have a question regarding this topic please email us at support@mindscape.co.nz
|
I have a LINQ query that is causing a crash in Lightspeed. It seems to work where there is only one entity in the database that matches the query, but when there are two, it crashes with a null pointer exception. The query I am running is the following (Ivan should have my complete model and test DB): // Find all products that match the passed in barcode. We only include active and NEW products. It is When I change one of the entities to have the same barcode in my unit test, in then crashes with this exception: System.Reflection.TargetInvocationException was unhandled by user code |
|
|
The exception (NullReferenceException) is being thrown from inside the select new {} projection which will being run post load. Its most likely coming from this line if there is no p.Location: LocationCode = p.Location.Barcode, or possibly this line: AllocatedBOQty = p.BackOrders.Where(bo => bo.DateShipped == null).Sum(bo => bo.QtyInStock) Are you able to isolate what aspect of the projection is causing the problem?
Jeremy |
|
|
Cool, I will try commenting out parts of the query until I figure out what it is. Thanks for the feedback! Part of the fun with LINQ (mashochistic fun that is!), is trying to figure out how to make LINQ work in practice. Tons of LINQ stuff will compile just fine and work on objects, but won't work when run against a database. I run into plenty of those issues with Entity Framework, and only got about 50% of my queries working at all with OpenAccess. |
|
|
Yes, ultimately a lot of it comes down to how the LINQ provider which is handling the queries operates. Its a bit like going back to VB6 with all the runtime errors that are possible :) If you havnt already you may find using LINQpad (www.linqpad.net) useful for prototyping queries - its a great tool for quickly executing LINQ queries against an actual provider (there is a plugin to target LightSpeed models: http://www.mindscapehq.com/blog/index.php/2010/03/01/lightspeed-and-linqpad-the-perfect-partners/).
Jeremy |
|
|
I played around a little bit with LINQPad, but had issues getting it working with DbContext in EF. I will give the Lightspeed connector a spin as that might help me figure this stuff out a little quicker and sort out where the queries are not working. |
|
|
Ok, I downloaded and installed LINQPad with the Lightspeed driver, and I can load my model just fine but when I got to run a query it give me this error: InvalidOperationException: The ConnectionString property has not been initialized. The connection string looks correct, as it came right out of my .lsmodel file (I let the connector figure it out from there). I am pretty sure my installed MySQL Connector/Net is 6.3.7, while the one Lightspeed is built against is 6.3.6. I need 6.3.7 installed for other reasons, so is there a way I can make sure LINQPad is using a local copy of 6.3.6? I think maybe that is the problem? |
|
|
It was this line that was the problem: LocationCode = p.Location.Barcode I changed it to: LocationCode = (p.Location == null) ? null : p.Location.Barcode and now it works. |
|