简体   繁体   English

为什么 Entity Framework Core 显示异步方法?

[英]Why Entity Framework Core is showing async methods?

I'm setting up a simple Web API on C# using Entity Framework Core.我正在使用 Entity Framework Core 在 C# 上设置一个简单的 Web API。

I tried to program a get() function but Entity Framework Core shows me FirstOrDefaultAsync() instead of FirstOrDefault() .我尝试FirstOrDefaultAsync() get()函数,但 Entity Framework Core 向我显示FirstOrDefaultAsync()而不是FirstOrDefault()

Why is this happening?为什么会这样?

Screenshot here截图在这里

Thanks!谢谢!

FirstOrDefault is part of a different namespace than FirstOrDefaultAsync. FirstOrDefault 是与 FirstOrDefaultAsync 不同的命名空间的一部分。

FirstOrDefaultAsync belongs to the Entity Framework Core package. FirstOrDefaultAsync 属于 Entity Framework Core 包。 FirstOrDefault belongs to Linq. FirstOrDefault 属于 Linq。

to get the FirstOrDefault method add this using statement:要获取 FirstOrDefault 方法,请添加以下 using 语句:

using System.Linq;

read these for more:阅读这些以了解更多信息:

https://docs.microsoft.com/en-us/dotnet/api/system.data.entity.queryableextensions.firstordefaultasync?view=entity-framework-6.2.0 https://docs.microsoft.com/en-us/dotnet/api/system.data.entity.queryableextensions.firstordefaultasync?view=entity-framework-6.2.0

https://docs.microsoft.com/en-us/dotnet/api/system.linq.enumerable.firstordefault?view=netcore-3.1 https://docs.microsoft.com/en-us/dotnet/api/system.linq.enumerable.firstordefault?view=netcore-3.1

FirstOrDefault is a linq query and it is used when dealing with a non asynchronous method while FirstOrDefaultAsync is used while using asynchronous method. FirstOrDefault 是一个 linq 查询,它在处理非异步方法时使用,而 FirstOrDefaultAsync 在使用异步方法时使用。 example where each of them where used you can as well make your method asynchronous in other to consume FirstOrDefaultAsync()例如,其中每个使用的地方您也可以使您的方法异步以使用 FirstOrDefaultAsync()

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM