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

7
  • I'm asking because EFCore auto makes those ICollections but they don't auto update.
    – Anastas.R
    Aug 16, 2018 at 9:05
  • What do you mean by Does EmployeeTeams auto update when i add to EmployeeTeams Table?
    – Vivek Nuna
    Aug 16, 2018 at 9:06
  • Does my Employee.EmployeeTeams update when I add to EmployeeTeam table :) Sorry for confusing you. @viveknuna
    – Anastas.R
    Aug 16, 2018 at 9: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, 2018 at 9: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, 2018 at 9:11

1 Answer 1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.