简体   繁体   English

实体框架C# - ObjectStateManager

[英]Entity Framework C# - ObjectStateManager

I am finding very hard to find a solution to this problem. 我发现很难找到解决这个问题的方法。 I have created an Edit.cshtml view in my MVC application. 我在MVC应用程序中创建了一个Edit.cshtml视图。 Currently in my Controller I have the following code 目前在我的控制器中,我有以下代码

[HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Edit(Vendor vendor)
    {
        if (ModelState.IsValid)
        {
            db.Vendors.Attach(vendor);
            db.ObjectStateManager.ChangeObjectState(vendor, EntityState.Modified);  
            db.Entry(vendor).CurrentValues.SetValues(vendor);
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        return View(vendor);
    }

But before i build the "dbo.ObjectStateManager" gives my an Error. 但在我构建“dbo.ObjectStateManager”之前,我给出了一个错误。 As below! 如下!

Error 25 'VendorScorecard.Models.VendorScorecardEntities1' does not contain a definition for 'ObjectStateManager' and no extension method 'ObjectStateManager' accepting a first argument of type 'VendorScorecard.Models.VendorScorecardEntities1' could be found (are you missing a using directive or an assembly reference?) C:\\solutions\\Web\\VisualStudio2010\\VendorScorecard\\VendorScorecard-Good\\VendorScorecard\\Controllers\\VendorController.cs 90 20 VendorScorecard 错误25'DeviceorScorecard.Models.VendorScorecardEntities1'不包含'ObjectStateManager'的定义,并且没有扩展方法'ObjectStateManager'可以找到类型为'VendorScorecard.Models.VendorScorecardEntities1'的第一个参数(您是否缺少using指令或者汇编参考?)C:\\ solutions \\ Web \\ VisualStudio2010 \\ VendorScorecard \\ VendorScorecard-Good \\ VendorScorecard \\ Controllers \\ VendorController.cs 90 20 VendorScorecard

i have tried thid line of code too! 我也尝试了一行代码! It removes the error but doesnt actually allow intput into my database 它删除了错误,但实际上不允许intput进入我的数据库

//db.Entry(vendor).State = EntityState.Modified; //db.Entry(vendor).State = EntityState.Modified;

From: 为什么我的db上下文中不存在ObjectStateManager属性?

var manager = ((IObjectContextAdapter)dbContext).ObjectContext.ObjectStateManager;

try this code it will resolve your problem 尝试此代码它将解决您的问题

db.Entry(employeeFromDB).State = EntityState.Modified;

so basically you have to get rid of one line and put this code instead please see below 所以基本上你必须摆脱一行,并把这个代码,请参阅下面

[HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Edit(Vendor vendor)
    {
        if (ModelState.IsValid)
        {
            db.Vendors.Attach(vendor);
             db.Entry(vendor).State = EntityState.Modified;
            db.Entry(vendor).CurrentValues.SetValues(vendor);
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        return View(vendor);
    }

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

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