Anastas.R
Anastas.R

Reputation: 101

Entity Framework Core update tables

I have mapped existing database in EF Core and got model classes associated with the tables.

For example: Employee class

 public int EmployeeId { get; set; }
 public string FirstName { get; set; }
 public string LastName { get; set; }

 public ICollection<EmployeeTeams> EmployeeTeams { get; set; }// all the teams employee is in

Teams class:

 public int TeamId { get; set; }
 public string TeamName { get; set; }
 public int? TeamLeaderId { get; set; }

 public Employees TeamLeader { get; set; }
 public ICollection<EmployeeTeams> EmployeeTeams { get; set; }

EmployeeTeams class:

 public int EmployeeId { get; set; }
 public int TeamId { get; set; }
 public Employees Employee { get; set; }
 public Teams Team { get; set; }

My question is: does EmployeeTeams auto update when I add to EmployeeTeams table (it has foreign keys to Employee and Teams table)?

Upvotes: 0

Views: 825

Answers (1)

Barr J
Barr J

Reputation: 10919

There is not auto-update in EF. When you decide to update an entity, you update the entity and no further updates will be made other than that. If they do, it will be a major security and performance issue, as some data you didn't want to update might get into your DB too and might result in a lot of unnecessary data and access to parts not meant to.

If you want to see the changes reflected on the entity after update, you use the Refresh method of Entity Framework.

If you want to do it to the absolute best, dispose of your entity and create a new one.

If you do not want to tamper with the context, it will load the new data next time you read from the database.

But the key word here, is Load and not Auto Update, those are two different terms.

There is a reason why you have to explicitly call context.SaveChanges() for every CUD operation in Entity Framework.

Upvotes: 1

Related Questions