简体   繁体   English

实体框架核心FromSQL引发ArgumentNullException

[英]Entity Framework core FromSQL throws ArgumentNullException

I'm using C# (.net core v2.2) and EF core. 我正在使用C#(.net core v2.2)和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). GetById是一个存储过程,用Azure SQL(基本上是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. 我猜它的成功率至少为99.9%。

UPDATE 1 更新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: 为了确定哪一行是有问题的(创建进程或运行FromSql方法),我添加了一条日志消息,并确定问题出在下面这样:

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

UPDATE 2 更新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. 我以为问题可能是由于我到数据库的映射-也许数据库模式已更改而未相应更改EF。 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. 再次重新生成类后,我发现自上次提交GIT以来,没有文件已更改。 Therefor, my EF code is already aligned with the database schema. 因此,我的EF代码已经与数据库架构对齐。

UPDATE 3 更新3

I was running my code with EF Nuget package version 2.2.1. 我使用EF Nuget软件包2.2.1版运行代码。 I saw that I can update the EF package to 2.2.2. 我看到可以将EF软件包更新为2.2.2。 I did it but it isn't solved the issue. 我做到了,但没有解决问题。 Same behavior. 行为相同。

What to do? 该怎么办? What else should I check? 我还应该检查什么?

You probably assume that $"EXEC GetById {id}" is a string , but it's actually a FormattableString , as stack trace shows: 您可能假设$"EXEC GetById {id}"是一个string ,但实际上是一个FormattableString ,如堆栈跟踪所示:

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: 尝试向string添加显式强制转换以执行所需的强制转换:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Entity Framework Core FromSql 模拟测试用例 - Entity Framework Core FromSql mock test cases 用干净的EF Core查询替换Entity Framework FromSql() - Replace Entity Framework FromSql() with clean EF Core query 实体框架ArgumentNullException - Entity Framework, ArgumentNullException Entity Framework Core 控制台应用程序 - 数据库连接生成 ArgumentNullException - Entity Framework Core console app - database connection generates ArgumentNullException 调用 FromSql 在 IQueryable 上不起作用<tentity>从 Entity Framework 核心 2.2 迁移到 3.0 之后</tentity> - Calling FromSql Not working on IQueryable<TEntity> after migration from Entity Framework core 2.2 to 3.0 使用FromSql方法(Linq)的实体框架核心输出参数在存储过程中不起作用 - Entity framework core Output parameter not working in store procedure using FromSql method (Linq) 实体框架7 FromSql存储过程返回值 - Entity Framework 7 FromSql stored procedure return value RemoveRange在实体框架核心中引发InvalidOperationException - RemoveRange throws InvalidOperationException in Entity Framework Core Entity Framework Core 添加具有已保存依赖项的实体会引发错误 - Entity Framework Core Add entity with dependecy that is saved throws the error 实体框架:2.2 版本中将字符串参数传递给 FromSql 语句 - Entity Framework: Passing String Parameter to FromSql Statement in Version 2.2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM