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?