I have two tables (Device and PropertyValue) and when I try to delete a Device I get an exception saying:
violates foreign key
I also tried to set the cascade on OnModelCreating(), but still not work.
public class Device
{
public int Id { get; set; }
public List<PropertyValue> Properties { get; set; }
}
public class PropertyValue
{
public int Id { get; set; }
public int? DeviceId { get; set; }
public Device Device { get; set; }
}
modelBuilder.Entity<PropertyValue>()
.HasOne(p => p.Device)
.WithMany(b => b.Properties)
.HasForeignKey(w => w.DeviceId)
.OnDelete(DeleteBehavior.Cascade);
EDIT: I am using a repository but basally I am deleting using _context.Devices.Remove(entity);