0

我在 mvc 应用程序中使用 ef 代码优先模型,我必须建模一个是产品 temp_product。我的产品编辑视图与产品模型绑定。显示用户所做的所有更改都将在产品模型中。现在在控制器中,我从产品模型中复制所有更新的值并将它们设置为 temp_product 并调用函数来更新数据库中的实体 temp_product。但正在发生的是我的产品表正在更新,而 temp_product 包含旧值。我该如何解决这个问题?

复制我使用的实体值..

public class Sync
{ 
   public static void CopyProperties(dynamic product, dynamic tempProduct) 
   { 
      PropertyInfo[] productPI = product.GetType().GetProperties(); 
      PropertyInfo[] tempProductPI =tempProduct.GetType().GetProperties(); 
      foreach (PropertyInfo temp_prodPI in tempProductPI) 
         foreach (PropertyInfo prodPI in productPI) 
         if (prodPI.Name == temp_prodPI.Name && temp_prodPI.CanWrite) 
         temp_prodPI.SetValue(tempProduct,prodPI.GetValue(product,nul‌​l), null); 
   }

_temp_productRepository.Update(temp_product);

并在更新功能

public void Update(T entity)
{
if (entity == null)throw new ArgumentNullException("entity"); 
this._context.SaveChanges();
}
4

0 回答 0