You are attaching the entity twice, remove the call to .Attach(role) and keep the line below it which is sufficient to add the object to the tracking context in a modified state.//_db.Roles.Attach(role); //REMOVE THIS LINE !._db.Entry(role).State = Microsoft.EntityFrameworkCore.EntityState.Modified;请注意,将条目的状态设置为已修改将在调用 .SaveChanges() 时更新所有属性值,因此如果您只想更新某些属性,请参阅 这个答案.Beware that setting the state of the entry to modified will update all the property values upon calling .SaveChanges(), so in case you want to update only certain properties refer to this answer.如果这不能解决您的问题,请检查您可能遗漏的任何内部异常.有时异常消息没有意义并掩盖了您可能能够在内部异常中找到的真正问题.If this doesn't solve your problem, please check for any inner exceptions that you might've missed. Sometimes the exception messages don't make sense and mask the real problem which you might be able to find in the inner exception. 这篇关于无法使用 EFCore 编辑数据库条目,EntityState.Modified:“数据库操作预计会影响 1 行,但实际上影响了 0 行."的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-29 20:54