samedi 26 janvier 2019

How to test whether my IQueryable will actually compile down to SQL

I have a project running on EF (6.0) and attempting to use OData to allow easy tabulation functionality.

In order to achieve this, we have lots of vertical slices that are handling IQueryable<T> right from the DbContext up to the Controller. We are mapping our domain models to VMs in those slices, which works because LinqToEntities is insanely powerful, and converts those C# object mapping statements into SQL. Our mapping method takes an IQueryable<DomainModel>, calls .Select() and returns an IQueryable<ViewModel>

But some of our mapping code is sufficiently complex that we're pushing the bounds of what LinqToEntities can handle, so sometimes we write code that compiles, but at runtime LinqToEntities tells us that it's too hard and isn't supported:

LINQ to Entities does not recognize the method 'System.String ToString(System.String)' method, and this method cannot be translated into a store expression.

That's fine and we're happy to work within those constraints. But I'd quite like to have a quick Unit Test that warns me about such errors.

How can I UnitTest whether my mapping code is compatible with LinqToEntities?


I was hoping that I could just run the mapping method in a unit test, with a stubbed out underlying IQueryable, but that doesn't seem to be working.

See here for a direct question about that error: Error when trying to test an isolated IQueryable

But I'm not certain that that would actually work, even if I could make the code execute?

What's the best way to achieve this goal?

Aucun commentaire:

Enregistrer un commentaire