user3340627
user3340627

Reputation: 3143

Clarification for entityframework function

What should the code below do? What is the purpose of adding the included table in this code, should it do a cascaded delete of the JobType related to the PersonId or will it only delete the Person with the specified PersonId ?

db.tblPerson.Remove(db.tblPerson.Include("tblJobType").FirstOrDefault(c => c.PersonId== PersonId));

Upvotes: 3

Views: 36

Answers (1)

Vlad274
Vlad274

Reputation: 6844

In this instance the .Include is either superfluous or really bad design.

Superfluous:

  • .Include is used to eagerly load related entities (Documentation), since this is immediately handed to the .Remove function, the related entity is never used, thus it is pointless.
  • If this is an attempt to delete the related entity as well, that will happen independent of loading the related entity. This effect is determined by the Cascade Delete setting of the relationship

Bad Design:

  • Because .Include will load the related entity, it necessitates a JOIN between the two tables. Depending on what the relationship between Person and JobType is (e.g. Optional), this could result in Persons being excluded (e.g. those without JobTypes).

Conclusion

The code posted is simply removing the Person with the specified PersonId. The .Include is likely just a copy-paste error that has no effect here. As noted above, this will not cause a CascadeDelete, that is determined by a different setting. However, you should be aware of the side-effects of .Include.

Upvotes: 2

Related Questions