Itso Dimitrov
Itso Dimitrov

Reputation: 3

Save changes dont save

have the following code -

var employees = context.Employees.Where(e => e.Department.Name == "Engineering" || e.Department.Name == "Tool Design" || e.Department.Name == "Marketing" || e.Department.Name == "Information Services")
                .OrderBy(e => e.FirstName)
                .ThenBy(e => e.LastName)
                .Select(e => new
                {
                    employeeFullInfo = $"{e.FirstName} {e.LastName} (${e.Salary * 12 / 100 + e.Salary:F2})"
                   //firstName = e.FirstName,
                   //lastName = e.LastName,
                   //Salary = e.Salary * 12 / 100 + e.Salary
                }).ToList();

 context.SaveChanges();

When i call saveChanges method nothing happened in database, why ? I want salary increase with 12 percentage on every employee, but in database nothing happened

Upvotes: 0

Views: 99

Answers (1)

TheGeneral
TheGeneral

Reputation: 81573

OK, your problem is you are not actually changing your entities. It's hard to see what you want to do, however, this might give you a push forward.

Select isn't the right thing here. Instead, just filter your employees:

var employees = context.Employees.Where(e => 
                      e.Department.Name == "Engineering" || 
                      e.Department.Name == "Tool Design" || 
                      e.Department.Name == "Marketing" || 
                      e.Department.Name == "Information Services");

// you need to modify each employee
foreach(var employee in employees)
{   
     // not sure this is correct, add pepper and salt to taste 
     employee.Salary = employee.Salary * 12 / 100 + e.Salary;
}

// then you can save
context.SaveChanges();

// List of infos
var listofInfo =  employees.OrderBy(e => e.FirstName)
                    .ThenBy(e => e.LastName)
                    .Select(e => $"{e.FirstName} {e.LastName} (${e.Salary:F2})")
                    .ToList();

foreach(var item in listofInfo)
   Console.WriteLine(item);

Upvotes: 4

Related Questions