错误而在ASP.NET MVC页更新数据库记录与实体框架 [英] Error while updating Database record with Entity Framework on ASP.NET MVC Page

查看:85
本文介绍了错误而在ASP.NET MVC页更新数据库记录与实体框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ASP.NET页面的更新注册用户详细地址选定的记录。
下面是我从控制器调用Update方法。

当我打电话的 ApplyPropertyChanges 方式,我收到以下错误。
有没有人遇到了同样的错误,同时更新与实体框架的纪录。

鸭preciate你的反应。

错误消息:

在ObjectContext中现有的对象是在增加的状态。当现有的对象是在未改变或修饰状态的变化只能应用于

我的更新方法code:

  [HttpPost]
    公共BOOL UpdateAddressDetail([装订(preFIX =RegUser)] AddressDetail regUserAddress,
                        的FormCollection FORMDATA)
    {
    regUserAddress.AD_Id = 3;
        regUserAddress.LastUpdated = HttpContext.User.Identity.Name;
        regUserAddress.UpdatedOn = DateTime.Now;
        regUserAddress.AddressType =((AddressDetail)会议[CurrentAddress])。地址类型? 主;
        。regUserAddress.Phone =((AddressDetail)会议[CurrentAddress])电话;
        regUserAddress.Country =((AddressDetail)会议[CurrentAddress])。地址类型? 美国;        miEntity.ApplyPropertyChanges(regUserAddress.EntityKey.EntitySetName,regUserAddress);
        miEntity.SaveChanges();
        返回true;
    }


解决方案

戴夫说。

+

您需要附上()断开实体的对象上下文:

http://msdn.microsoft.com/en-us/library/system.data.objects.objectcontext.attach.aspx

  miEntity.Attach(regUserAddress);
miEntity.SaveChanges();

I have an ASP.NET Page that updates registered User Address Details for a selected record. Below is the Update method that i am calling from Controller.

When i am calling ApplyPropertyChanges method, I am getting the below error. Did anyone run into the same error while updating the record with Entity Framework.

Appreciate your responses.

Error Message:

The existing object in the ObjectContext is in the Added state. Changes can only be applied when the existing object is in an unchanged or modified state.

My Update Method Code:

[HttpPost]
    public bool UpdateAddressDetail([Bind(Prefix = "RegUser")] AddressDetail regUserAddress, 
                        FormCollection formData)
    {
    regUserAddress.AD_Id = 3;
        regUserAddress.LastUpdated = HttpContext.User.Identity.Name;
        regUserAddress.UpdatedOn = DateTime.Now;
        regUserAddress.AddressType = ((AddressDetail)Session["CurrentAddress"]).AddressType ?? "Primary";
        regUserAddress.Phone = ((AddressDetail)Session["CurrentAddress"]).Phone;
        regUserAddress.Country = ((AddressDetail)Session["CurrentAddress"]).AddressType ?? "USA";

        miEntity.ApplyPropertyChanges(regUserAddress.EntityKey.EntitySetName, regUserAddress);
        miEntity.SaveChanges();
        return true;
    }

解决方案

What Dave Said

+

You need to Attach() the disconnected entity to your object context:

http://msdn.microsoft.com/en-us/library/system.data.objects.objectcontext.attach.aspx

miEntity.Attach(regUserAddress);
miEntity.SaveChanges();

这篇关于错误而在ASP.NET MVC页更新数据库记录与实体框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆