Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何通过传入对象使用实体框架进行更新_C#_Entity Framework_Asp.net Mvc 4 - Fatal编程技术网

C# 如何通过传入对象使用实体框架进行更新

C# 如何通过传入对象使用实体框架进行更新,c#,entity-framework,asp.net-mvc-4,C#,Entity Framework,Asp.net Mvc 4,如何使用实体框架进行更新?我正在传递带有更新值的对象,但没有看到更新方法 public void UpdateRecipient(Domain.Entities.RecipientEntity recipient) { using (EfDbContext context = CreateEfDbContext(recipient.ApplicationId.ToString())) { context.Recipients

如何使用实体框架进行更新?我正在传递带有更新值的对象,但没有看到更新方法

   public void UpdateRecipient(Domain.Entities.RecipientEntity recipient)
    {
        using (EfDbContext context = CreateEfDbContext(recipient.ApplicationId.ToString()))
        {

            context.Recipients. //?? I don't see an update method
            context.SaveChanges();

        }
    }
三个步骤:

  • 从上下文中获取要更新的项
  • 从传递更新方法的实体复制更新的属性
  • 保存更改
  • 大致:

    using (EfDbContext context = CreateEfDbContext(recipient.ApplicationId.ToString()))
    {
        var toUpdate = context.Recipients.SingleOrDefault(r => r.Id == recipient.Id);
        if (toUpdate != null)
        {
            toUpdate.Field1 = recipient.Field1;
            // Map over any other field data here.
    
            context.SaveChanges();
        }
        else
        {
            // Handle this case however you see fit.  Log an error, throw an error, etc...
        }
    }
    
    三个步骤:

  • 从上下文中获取要更新的项
  • 从传递更新方法的实体复制更新的属性
  • 保存更改
  • 大致:

    using (EfDbContext context = CreateEfDbContext(recipient.ApplicationId.ToString()))
    {
        var toUpdate = context.Recipients.SingleOrDefault(r => r.Id == recipient.Id);
        if (toUpdate != null)
        {
            toUpdate.Field1 = recipient.Field1;
            // Map over any other field data here.
    
            context.SaveChanges();
        }
        else
        {
            // Handle this case however you see fit.  Log an error, throw an error, etc...
        }
    }
    

    如果要更新记录,请执行以下操作:

    //Retrieve the entity to be updated
    Entity row = context.Recipients.Single(a => a.Id == recipient.Id);
    
    //Update a column
    row.Name = recipient.Name;
    
    //Save changes
    context.SaveChanges();
    
    如果要同时更新/添加内容,请执行以下操作:

    if(!context.Recipients.Any(a => Id == recipient.Id))
    {
        context.Recipients.Add(recipient);
    }
    else
    {
        Entity row = context.Recipients.Single(a => a.Id == recipient.Id);
    
        row.Name = recipient.Name;
    }
    
    context.SaveChanges();
    

    如果要更新记录,请执行以下操作:

    //Retrieve the entity to be updated
    Entity row = context.Recipients.Single(a => a.Id == recipient.Id);
    
    //Update a column
    row.Name = recipient.Name;
    
    //Save changes
    context.SaveChanges();
    
    如果要同时更新/添加内容,请执行以下操作:

    if(!context.Recipients.Any(a => Id == recipient.Id))
    {
        context.Recipients.Add(recipient);
    }
    else
    {
        Entity row = context.Recipients.Single(a => a.Id == recipient.Id);
    
        row.Name = recipient.Name;
    }
    
    context.SaveChanges();
    

    还有一种方法可以更新对象,而无需再次从数据库中重新获取对象,从而节省了访问数据库的成本。要附加的对象的主键必须有一个值

  • 将更新的对象附加到上下文
  • 将其状态更改为“已修改”
  • 调用上下文的
    SaveChanges()
    方法
  • 比如:


    还有一种方法可以更新对象,而无需再次从数据库中重新获取对象,从而节省了访问数据库的成本。要附加的对象的主键必须有一个值

  • 将更新的对象附加到上下文
  • 将其状态更改为“已修改”
  • 调用上下文的
    SaveChanges()
    方法
  • 比如: