简体   繁体   English

检查记录是否存在返回“序列包含多个匹配元素”。 对于带有实体框架 5 的 Net5

[英]Checking if a record exist returns "Sequence contains more than one matching element." for Net5 with Entity Framework 5

I have an .Net5 solution using Entity Framework 5.0.0.我有一个使用 Entity Framework 5.0.0 的 .Net5 解决方案。 Im able to save data to the Backend SQL DB and all is well.我能够将数据保存到后端 SQL 数据库,一切都很好。 But when I try to check is a record exist using the statement below im getting an error " Sequence contains more than one matching element. "但是当我尝试使用下面的语句检查是否存在记录时,我得到一个错误“序列包含多个匹配元素。

 var status = await dbContext.Item.AnyAsync(x => x.Name == "Bread")

I'm not sure what's causing this.我不确定是什么原因造成的。 Any help appreciated任何帮助表示赞赏

Im using the following packages我使用以下软件包

    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="5.0.0" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="5.0.0">
 <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="5.0.0">

This is not an error that should come back from AnyAsync .这不是应该从AnyAsync返回的错误。 I suspect there is something in the structure of your items (eg via a foreign key) that is generating the error.我怀疑您的项目结构中有某些东西(例如通过外键)会产生错误。 Use ToTraceString() to find out the underlying SQL query.使用ToTraceString()找出底层 SQL 查询。

I solved it by updating all my EntityFramework related packages to我通过将所有与 EntityFramework 相关的包更新为

<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="5.0.17" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="5.0.17">
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="5.0.17">

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

相关问题 实体框架迁移:序列包含多个匹配元素 - Entity Framework Migration: Sequence contains more than one matching element 实体框架 | 序列包含多个匹配元素 - Entity Framework | Sequence contains more than one matching element Sequence包含多个匹配元素 - Sequence contains more than one matching element 序列包含多个匹配元素-使用Entiity Framework添加项目 - Sequence contains more than one matching element - Adding item with Entiity Framework 序列在实体框架代码优先迁移中包含多个元素错误 - Sequence contains more than one element error in Entity framework code-first migrations 序列在批量插入上包含多个匹配元素 - sequence contains more than one matching element on Bulk insert Linq InvalidOperationException:序列包含多个匹配元素 - Linq InvalidOperationException: Sequence contains more than one matching element 序列包含多个匹配的元素,其guid为ID - Sequence contains more than one matching element with guid as ID SingleOrDefault()引发异常序列包含多个匹配元素 - SingleOrDefault() Throw exception Sequence contains more than one matching element 努力抛出 InvalidOperationException:序列包含多个匹配元素 - Effort throws InvalidOperationException: Sequence contains more than one matching element
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM