gpt4 book ai didi

c# - 使用 EntityState.Modified 时出错

转载 作者:太空宇宙 更新时间:2023-11-03 20:07:45 24 4
gpt4 key购买 nike

我正在关注 this在 Web API 项目中实现存储库模式的文章。在存储库中实现更新方法时,我遵循了提供的示例,使用:

context.Entry<Item>(item).State = System.Data.EntityState.Modified;

由于某些原因,虽然我遇到了以下构建错误,

an instance of type system.data.entitystate can not be assigned to a variable of type system.data.entity.entitystate

除了我使用的实体与示例中使用的实体不同之外,我的代码与示例中的代码相同。任何想法为什么我可能会收到此错误?我正在使用 EF 6.02。

干杯

最佳答案

错误信息很清楚,你使用了错误的类型,你只需要这样写:

context.Entry<Item>(item).State = System.Data.Entity.EntityState.Modified;

System.Data.Entity.EntityState 存在于 EF 的 EntityFramework.dll 中,而 System.Data.EntityState 存在于System.Data.Entity.dll

自 EF6 起,无需引用 System.Data.Entity.dll(其中包含 EntityFramework 的某些部分,这些部分在以前的 EF 版本中需要)。

因此,您只需删除 System.Data.Entity.dll 引用(它在创建新项目时由 Visual Studio 自动添加)以避免将来出现这些类型不匹配错误。

关于c# - 使用 EntityState.Modified 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21975035/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com