0

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)?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Anastas.R
  • 101
  • 8
  • I'm asking because EFCore auto makes those ICollections but they don't auto update. – Anastas.R Aug 16 '18 at 09:05
  • What do you mean by Does EmployeeTeams auto update when i add to EmployeeTeams Table? – Vivek Nuna Aug 16 '18 at 09:06
  • Does my Employee.EmployeeTeams update when I add to EmployeeTeam table :) Sorry for confusing you. @viveknuna – Anastas.R Aug 16 '18 at 09:08
  • Do you mean does it add the relationship when there was none before when you assign it? Or do you worry that it will overwrite changes done by someone else when you just update a team? EF Core does track entities when you read it from the DB and track the **CHANGES**. Only the changes will be updated (or added, removed). When you do nothing on `EmployeeTeams` collection, nothing will be changed – Tseng Aug 16 '18 at 09:08
  • I'm asking about this scenario: I add and employee, his Employee.EmployeeTeams is empty. When I add an entry to EmployeeTeams table and is the id of that employe that i created does his Employee.EmployeeTeams get updated. – Anastas.R Aug 16 '18 at 09:11
  • It will **auto-update** the next time you actually **read** from the database. The database of course cannot "auto-update" any of your in-memory collections...... – marc_s Aug 16 '18 at 10:00
  • Please add some code that executes what you want to do: add an `Employee` instance, add an `EmployeeTeams` instance. Show *exactly* where you save changes. The influence of timing and sequence of all these actions is too specific to describe in general terms. Without this, the question is "unclear what you're asking". – Gert Arnold Aug 16 '18 at 22:19

1 Answers1

1

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.

el_M
  • 149
  • 15
Barr J
  • 10,636
  • 1
  • 28
  • 46