在ASP上使用实体框架更新数据库记录时出错。净MVC页面

时间:2022-06-10 15:42:03

I have an ASP.NET Page that updates registered User Address Details for a selected record.

我有一个ASP。更新所选记录的已注册用户地址详细信息的NET页面。

Below is the update method that I am calling from my controller.

下面是我正在从我的控制器调用的更新方法。

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

当我调用ApplyPropertyChanges方法时,会得到一个错误。是否有人在使用实体框架更新记录时遇到了相同的错误?

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.

ObjectContext中的现有对象处于添加状态。只有当现有对象处于未更改或修改状态时,才能应用更改。

My Update method:

我的更新方法:

[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;
}

4 个解决方案

#1


1  

The error is the object is detached from the context, and ApplyPropertyChanges thinks the object is added because it isn't attached. So you would need to query from the data context or get an attached form and then apply the changes then.

错误在于对象与上下文分离,而ApplyPropertyChanges认为对象被添加是因为它没有附加。因此,您需要从数据上下文中查询或获得附加的表单,然后应用更改。

HTH.

HTH。

#2


1  

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

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

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

#3


0  

Just add the following code before miEntity.SaveChanges():

只需在miEntity.SaveChanges()之前添加以下代码:

miEntity.Entry(regUserAddress).State = EntityState.Modified;

#4


0  

First select the record (object entity), search by key through the ObjectContext. For example if the search ArticleSet EntitySet called for there to record, and once you get it modified its properties with new values and then call SaveChanges() of ObjectContext.

首先选择记录(对象实体),通过ObjectContext按键搜索。例如,如果搜索ArticleSet EntitySet需要在那里进行记录,一旦您让它使用新值修改其属性,然后调用ObjectContext的SaveChanges()。

Example:

例子:

ObjectQuery<Article> myArt=Context.ArticleSet.Where myArt = (row => row.ArticleId == value);
myArt.Description=" new value ";
etc. ..
etc ...

Context.SaveChanges ();

#1


1  

The error is the object is detached from the context, and ApplyPropertyChanges thinks the object is added because it isn't attached. So you would need to query from the data context or get an attached form and then apply the changes then.

错误在于对象与上下文分离,而ApplyPropertyChanges认为对象被添加是因为它没有附加。因此,您需要从数据上下文中查询或获得附加的表单,然后应用更改。

HTH.

HTH。

#2


1  

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

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

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

#3


0  

Just add the following code before miEntity.SaveChanges():

只需在miEntity.SaveChanges()之前添加以下代码:

miEntity.Entry(regUserAddress).State = EntityState.Modified;

#4


0  

First select the record (object entity), search by key through the ObjectContext. For example if the search ArticleSet EntitySet called for there to record, and once you get it modified its properties with new values and then call SaveChanges() of ObjectContext.

首先选择记录(对象实体),通过ObjectContext按键搜索。例如,如果搜索ArticleSet EntitySet需要在那里进行记录,一旦您让它使用新值修改其属性,然后调用ObjectContext的SaveChanges()。

Example:

例子:

ObjectQuery<Article> myArt=Context.ArticleSet.Where myArt = (row => row.ArticleId == value);
myArt.Description=" new value ";
etc. ..
etc ...

Context.SaveChanges ();