rumblefx0
rumblefx0

Reputation: 673

How can I delete EF entities in a performant way?

I have the following (simplified) EF model, which I am accessing with RIA services:

public class Employee
{
    public long Id { get; set; }
    public string Name { get; set; }
}

public class Department
{
    public long Id { get; set; }
    public string Name { get; set; }
    public List<Employee> { get; set;}
}

I would expect when I delete a department from my DomainContext, that the referenced Employees are deleted as well. Basically a cascade delete, handled by EF.

Now I found 2 ways to achieve this:

1. Enable Cascade Delete at the database level:

I'd really, really not like to do this if possible at all.

2. Load the child entities (Employees) at runtime (currently implemented):

When the user wants to delete a Department, the Employees are loaded into a list, iterated over and deleted from the context. I cannot help but feel this is clunky, it works however...

Any thoughts on the proper way to handle this? Thanks!

Upvotes: 0

Views: 117

Answers (1)

Ladislav Mrnka
Ladislav Mrnka

Reputation: 364409

You mentioned both ways supported by EF. If you don't like them you need stored procedure - there is no better way. In case of EF most common solution is cascade delete.

Upvotes: 3

Related Questions