Entity Framework core FromSQL throws ArgumentNullException

c# entity-framework entity-framework-core

Question

I'm using C# (.net core v2.2) and EF core. I have pretty old code that is retrieving a row from the database:

string id = "123";
using (var context = new UsersDb())
{
    var req = context.ScanRequests.FromSql($"EXEC GetById {id}").FirstOrDefault();
    // ...
}

GetById is a stored procedure, written in Azure SQL (basically - SQL Server).

This code started to throw an exception recently:

System.ArgumentNullException: Value cannot be null.
Parameter name: key
   at System.Collections.Generic.Dictionary`2.FindEntry(TKey key)
   at System.Collections.Generic.Dictionary`2.TryGetValue(TKey key, TValue& value)
   at lambda_method(Closure , ServiceProviderEngineScope )
   at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)
   at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider)
   at Microsoft.EntityFrameworkCore.DbContext.get_DbContextDependencies()
   at Microsoft.EntityFrameworkCore.DbContext.get_InternalServiceProvider()
   at Microsoft.EntityFrameworkCore.DbContext.get_DbContextDependencies()
   at Microsoft.EntityFrameworkCore.DbContext.get_Model()
   at Microsoft.EntityFrameworkCore.Internal.InternalDbSet`1.get_EntityType()
   at Microsoft.EntityFrameworkCore.Internal.InternalDbSet`1.get_EntityQueryable()
   at Microsoft.EntityFrameworkCore.Internal.InternalDbSet`1.System.Linq.IQueryable.get_Provider()
   at Microsoft.EntityFrameworkCore.RelationalQueryableExtensions.FromSql[TEntity](IQueryable`1 source, FormattableString sql)
   at ***.***Factory.<>c__DisplayClass0_0.<GetById>b__0() in /tmp/sources/SystemLayer/***/***.cs:line 29

This issue doesn't happen to me when I run the same code with the same parameters in a local environment. Also, in most cases it's also working on production servers. I guess it's at least 99.9% success rate.

UPDATE 1

In order to make sure which is the problematic line (creating the process OR running the FromSql method), I added a log messages and found that the problem is in this like for sure:

var req = context.ScanRequests.FromSql($"EXEC GetById {id}").FirstOrDefault();

UPDATE 2

I thought maybe the problem is because of my mapping to the database - maybe the database schema has been changed without changing the EF accordingly. So I run the mapping command again:

Scaffold-DbContext "cs" ...

After re-generating the classes again, I found that no file has been changed since my last GIT commit. Therefor, my EF code is already aligned with the database schema.

UPDATE 3

I was running my code with EF Nuget package version 2.2.1. I saw that I can update the EF package to 2.2.2. I did it but it isn't solved the issue. Same behavior.

What to do? What else should I check?

1
1
3/12/2019 6:11:44 AM

Popular Answer

You probably assume that $"EXEC GetById {id}" is a string, but it's actually a FormattableString, as stack trace shows:

at Microsoft.EntityFrameworkCore.RelationalQueryableExtensions.FromSql[TEntity](IQueryable`1 source, FormattableString sql)

So, you accidentally call another method. Try to add explicit cast to string to execute desired one:

var req = context.ScanRequests.FromSql((string)$"EXEC GetById {id}").FirstOrDefault();

2
3/12/2019 11:28:29 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow