Witam.
Mam pewien problem.
Przy próbie usunięcia klienta z bazy danych dostaję taki wyjątek:
InnerException {"The DELETE statement conflicted with the REFERENCE constraint \"FK_dbo.Devices_dbo.Clients_OwnerId\". The conflict occurred in database \"Testowa_v4\", table \"dbo.Devices\", column 'OwnerId'.\r\nThe statement has been terminated."} System.Exception {System.Data.SqlClient.SqlException}
Upraszczając, mam taką strukturę bazy:
public class Device
{
public int Id { get; set; }
[...]
public int? OwnerId { get; set; }
public DateTime? ModifiedDate { get; set; }
public int? ModifiedUserId { get; set; }
[..]
public virtual Client Owner { get; set; }
public virtual User ModifiedUser { get; set; }
}
public class Client
{
public int Id { get; set; }
public string Name { get; set; }
public string Firstname { get; set; }
public string Lastname { get; set; }
public virtual ICollection<Device> Devices { get; set; }
}
I teraz chcę usunąć klienta jednak dostaję taki wyjątek. Mógłbym zrobić tak:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Client>()
.HasMany(c => c.Devices)
.WithOptional(d => d.Owner)
.WillCascadeOnDelete(true);
}
Ale wtedy kaskadowo usunie mi wszystkie jego urządzenia, a o dalszych zależnościach od urządzenia nie mówiąc. Chciałbym, aby po usunięciu klienta w urządzeniu podstawiało mi za niego po prostu nulla. Ktoś mógłby podpowiedzieć?