无法隐式转换类型'System.Data.EntityState'到 'System.Data.Entity.EntityState'。存在显式转换(是否缺少转换?)

17

在使用Entity Framework时,我遇到了以下错误:“Cannot implicitly convert type System.Data.EntityState to System.Data.Entity.EntityState. An explicit conversion exists (are you missing a cast?)”。

以下是代码片段:

foreach (OrderLine line in order.OrderLines)
{
    context.Entry(line.Product).State = System.Data.EntityState.Modified;
}
请建议我应该做些什么来解决这个错误。

1
context 是什么类型? - trailmax
3个回答

48

2
当添加一个继承自“Web API 2 OData Controller with actions, using Entity Framework”的新控制器时,生成的代码包含db.Entry(application).State = EntityState.Modified,应该将其更改为db.Entry(application).State = Entity.EntityState.Modified - D. Kermott

1

这在我的一个生成的控制器中发生了。在删除 using System.Data.EntityState 并添加 using Microsoft.EntityFrameworkCore 后,它对我起作用了。


0

你的代码必须像这样:

if (ModelState.IsValid)
{
    db.Entry(movie).State = System.Data.Entity.EntityState.Modified;
    db.SaveChanges();
    return RedirectToAction("Index");
}
return View(movie);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接