1

Let us assume I have the following classes which generated me tables:

public class Car
{
    public Guid Id { get; set; }
    public Guid OwnerId { get; set; }
    public string Color { get; set; }

    public virtual Owner Owner { get; set; }
}


public class Owner
{
    public Guid Id { get; set; }
    public string Name { get; set; }

    public ICollection<Car> Cars { get; set; }
}

In my DbContext class I am creating a method to remove a car. I would like to know what is the difference between the following two approaches and if it matters which approach I use:

Approach 1:

public class CarDbContext : DbContext
{
    public DbSet<Car> Car { get; set; }
    public DbSet<Ownerr> Owner { get; set; }

    public CarDbContext(string cs) : base (cs) {}

    public void RemoveCar(Car car)
    {
        Car.Attach(car);
        Car.Remove(car);
        SaveChanges();
    }
}

Approach 2:

public class CarDbContext : DbContext
{
    public DbSet<Car> Car { get; set; }
    public DbSet<Ownerr> Owner { get; set; }

    public CarDbContext(string cs) : base (cs) {}

    public void RemoveCar(Car car)
    {
        Entry(car).State = EntityState.Deleted;
        SaveChanges();
    }
}

What if I want to remove Owner now, since car is dependent, will the approach I use matter now?

Bagzli
  • 6,254
  • 17
  • 80
  • 163

1 Answers1

1

what is the difference between the following two approaches and if it matters which approach I use

Assuming the code you are actually working with is similar to what you have above, there is not much difference because you are saving immediately and you don't have any dependents, but I believe there is still a bit of difference you may want to be aware of for the future.

Approach 1: Attaching, Removing, Saving

When Remove() is called, EF will stop change tracking, mark your entity as deleted, and do some additional work with any related entities and the relationships.

Approach 2: Marking as Deleted

Your entity is marked as deleted. When SaveChanges() is called, EF generates and executes a delete query. Assuming no foreign key violations happen, the record is gone. I do not believe that additional work mentioned above is done, but I don't recall.

What if I want to remove Owner now, since car is dependent, will the approach I use matter now?

For this part of your question, this answer explains the different approaches pretty well and may provide additional clarification from what I have explained.

Community
  • 1
  • 1
ninja coder
  • 1,067
  • 9
  • 22
  • To clarify, if I call `Owner.Remove(owner)` the code would crash because there is a record in Car table that is dependent on that owner. If I call `Entry(owner).State = EntityState.Deleted;` then the owner would be removed and I would have an orphaned record in car table that now has no owner. Is this correct? I based this on what I read in the link you provded. – Bagzli Mar 19 '17 at 20:17
  • This all depends on how your database keys are setup and how your entities are configured. Remove() is less likely to throw an exception because it should take care of the child entities by either removing them or nulling the foreign key whereas EntityState.Deleted will cause the database to throw an exception – ninja coder Mar 20 '17 at 02:22
  • Your best option is to setup a sandbox with your context pointing to a local instance of SQL and try out each approach to verify the behavior – ninja coder Mar 20 '17 at 02:29