I have a table called Customers. On my website, the customer fills in his first name, last name, and email. I want to check if the email already exists in the database by what the customer fills in. If the customer exists, I would like to update the first name and last name (if they are different from the database).
The code I'm currently using for this is as following:
var email = vm.Reservation.Customer.Email;
var customer = db.Customers.Where(x => x.Email == email).First();
if(customer != null)
{
db.Entry(customer).State = !db.Customers.Any(c => c.Email == customer.Email) ? EntityState.Added : EntityState.Modified;
} else {
db.Customers.Add(vm.Reservation.Customer);
}
vm.Save();
However, it results in just adding a new customer with the modified username and password in the database with the same email.
How can I achieve this?
Thanks in advance.