---
title: "Entity Framework 4.0 Insert/Update"  
description: "Entity Framework 4.0 Insert/Update"  
author: "Anonymous User"  
published: 2013-09-28  
updated: 2013-09-28  
canonical: https://www.mindstick.com/forum/1545/entity-framework-4-0-insert-update  
category: "asp.net mvc"  
tags: ["asp.net mvc"]  
reading_time: 2 minutes  

---

# Entity Framework 4.0 Insert/Update

I was hoping there is a way to use the same code to both [insert](https://www.mindstick.com/blog/173/executing-insert-delete-or-update-query-in-sqlserver-using-ado-dot-net) and [update](https://www.mindstick.com/forum/156353/what-is-hummingbird-update).

For example, I insert a new [client](https://www.mindstick.com/articles/23198/3-steps-to-ensure-that-your-client-portal-is-impeccable) like the below using the [entity framework](https://www.mindstick.com/articles/1566/crud-operations-using-entity-framework-code-first-approach).

\

```
using (var context = new GenWebEntities())
{
  clientObj = new Client();
  var client = new Client
  {
    FirstName = "John",
    LastName = "Smith"
  };
  clientObj = client;
  context.SaveChanges();
}
```

And If I could use the same code to update the client as well, I would not have to write **double** the code.

```
using (var context = new GenWebEntities())
{
  clientObj = context.Clients.Single(c => c.ClientId == 31);
  var client = new Client
  {
    FirstName = "Johnathan", // Updated First Name
    LastName = "Rock" // Updated Last name
  };
  clientObj = client;
  context.SaveChanges();
}
```

If there is one way to code for both the insert and update, I would love to know how as this [method](https://www.mindstick.com/forum/166/webservice-method) above just inserts a new record.

** UPDATE ** The [problem](https://yourviews.mindstick.com/view/81399/tackling-the-problem-of-unemployment-during-corona-pandemic) I run into with this solution is I have a [one to many](https://www.mindstick.com/interview/820/how-to-implement-one-to-one-one-to-many-and-many-to-many-relationships-while-designing-tables) relationship.

## Example:

I have another [table](https://www.mindstick.com/articles/43918/how-to-design-table-using-bootstrap) ClientPayment that is one to many. So when I need to update the ClientPayment how do I [reference](https://www.mindstick.com/forum/774/reference-what-does-this-error-mean-in-php) this table for the update?

For the insert I would normaly do:

```
client.ClientPayments.Add(new ClientPayment { Type = "Credit Card", CardNumber = "4111-1111-1111-1111")
```

Any Help Will Be Appreciate!

## Replies

### Reply by Anonymous User

Hey Goti!

You should do something like this:

```
private void SaveAndUpdate(int? ClientId)
{
    using (var context = new GenWebEntities())
    {
        Client client;
        if(ClientId.HasValue)
            client=context.Clients.Single(c => c.ClientId == ClientId.Value);
        else
            client = new Client();
        client.FirstName = "Johnathan";
        client.LastName = "Rock";
        if(!ClientId.HasValue)
            context.Clients.Add(client)
        context.SaveChanges();
    }
}
```


---

Original Source: https://www.mindstick.com/forum/1545/entity-framework-4-0-insert-update

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
