I have a simple EF Core model with two entities, Location, and Address where a Location must always have an Address.
The entities and their configuration is as follow:
public class Location {
[Key]
public int Id { get; set; }
public string Name { get; set; }
public int AddressId { get; set; }
[ForeignKey("AddressId")]
public virtual Address Address { get; set; }
}
public class Address {
public virtual int AddressId { get; set; }
public string LineOne { get; set; }
public string LineTwo { get; set; }
public string City { get; set; }
public string Country { get; set; }
public string ZipPostCode { get; set; }
public string State { get; set; }
public string LandlinePhone { get; set; }
public string MobilePhone { get; set; }
}
// Context configuration
...
protected override void OnModelCreating(ModelBuilder modelBuilder) {
...
modelBuilder.Entity<Location>().HasOne(l => l.Address).WithOne().OnDelete(DeleteBehavior.Cascade);
...
}
...
Note: The address entity does not have a reference back to the location.
With the above configuration, deleting a location should delete its address but deleting the address should result in an reference integrity error. Trying the above with a unit test using EF InMemory database gives an unexpected outcome:
[Test]
public void DeleteAddressShouldNotDeleteLocation() {
var address = new Address();
var location = new Location() {Address = address};
_context.Locations.Add(location);
_context.SaveChanges();
// Delete the address added above
_context.Addresses.Remove(_context.Addresses.Single());
_context.SaveChanges();
Assert.AreEqual(1, _context.Locations.Count()); --> ALWAYS FAILS AS THE LOCATION IS CASCADE DELETED
}
This also happens on our production server (Azure SQL) so it shouldn't be an issue with EF Core.
Is there a reason why EF is cascading the address deletion to the Location?