2017-03-29 2 views
1

Ich habe eine Code-First-EF-Datenbank-Lösung. Die folgenden sind meine Modellklassen:Entity Framework, Löschen von einer Sammlung führt zu Fremdschlüssel Ausnahme

public class HolidayAllowance 
{ 
    private decimal _taken; 

    [Key] 
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)] 
    public int Id { get; set; } 
    [Required] 
    public int EmployeeId { get; set; } 
    public decimal Allowance { get; set; } 
    public virtual ObservableCollection<Holiday> Holidays { get; set; } 

    public decimal Taken 
    { 
     get { return Holidays?.Sum(x => x.Duration) ?? 0; } 
     set { _taken = value; } 
    } 

    public decimal Remaining => Allowance - Taken; 
} 

public class Holiday 
{ 
    [Key] 
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)] 
    public int Id { get; set; } 
    public DateTime HolidayStartDay { get; set; } 
    public HolidayType HolidayStartDayType { get; set; } 
    public decimal Duration { get; set; } 
    public int HolidayAllowanceId { get; set; } 
} 

Mein DbContext:

public class StaffAndInvoiceManagerDbContext : DbContext 
{ 
    public DbSet<Customer> Customers { get; set; } 
    public DbSet<Employee> Employees { get; set; } 
    public DbSet<HolidayAllowance> HolidayAllowances { get; set; } 
    public DbSet<Note> Notes { get; set; } 
    public DbSet<Termination> Terminations { get; set; } 
} 

Das Verfahren von meinem DataService, die einen Urlaub von der holidayAllowance zu löschen versucht:

public void DeleteHoliday(Holiday selectedHoliday) 
{ 
    var allowance = _dbContext 
     .HolidayAllowances 
     .FirstOrDefault(x => x.Id == selectedHoliday.HolidayAllowanceId); 

    allowance? 
     .Holidays 
     .Remove(selectedHoliday); 

    _dbContext.SaveChanges(); 
} 

Die Methode auf meinem ViewModel, das das ruft:

private void DeleteHoliday() 
{ 
    if (!_messageBoxService.AskYesNoQuestion("Delete Holiday?", "Do you want to delete this holiday?")) return; 

    _staffDataService.DeleteHoliday(SelectedHoliday); 
    HolidayAllowance.Holidays.Remove(SelectedHoliday); 
    RaisePropertyChanged(nameof(HolidayAllowance)); 
} 

Das Problem ist, wenn ich versuche, ein Holiday aus der HolidayAllowance.Holidays Sammlung löschen ich die folgende Fehlermeldung erhalten:

The operation failed: The relationship could not be changed because one or more of the foreign-key properties is non-nullable. When a change is made to a relationship, the related foreign-key property is set to a null value.
If the foreign-key does not support null values, a new relationship must be defined, the foreign-key property must be assigned another non-null value, or the unrelated object must be deleted.

Ich verstehe nicht, warum das eine FK Verletzung sein würde? Ich versuche nur, aus einer Sammlung zu löschen.

Meine DB-Tabellen, die EF generiert.

enter image description here

Ich kann die folgende SQL ausführen und es funktioniert, ohne eine FK Ausnahme zu werfen.

delete from dbo.Holidays 
where Id = 2 

Der Fremdschlüssel seiner Erzeugungs sieht wie folgt aus: Wenn ich Skript in SSMS erstellen

ALTER TABLE [dbo].[Holidays] WITH CHECK ADD CONSTRAINT [FK_dbo.Holidays_dbo.HolidayAllowances_HolidayAllowanceId] 
FOREIGN KEY([HolidayAllowanceId]) 
REFERENCES [dbo].[HolidayAllowances] ([Id]) 
GO 

ALTER TABLE [dbo].[Holidays] CHECK CONSTRAINT [FK_dbo.Holidays_dbo.HolidayAllowances_HolidayAllowanceId] 
GO 

ich folgendes in meinem DbContext versucht haben nach der Lektüre @Fals Kommentar unten.

protected override void OnModelCreating(DbModelBuilder modelBuilder) 
{ 
    modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>(); 
    modelBuilder.Conventions.Remove<ManyToManyCascadeDeleteConvention>(); 
    base.OnModelCreating(modelBuilder); 
} 

Ich hätte gedacht, EF war ähnlich? Immer noch kein Glück.

+0

Überprüfen Sie, ob Cascade aktiviert ist. Es versucht wahrscheinlich, auch andere Abhängigkeiten zusammen zu löschen. – Fals

+0

@Fals, ich habe die Frage aktualisiert. Ich habe die Konventionen zum Entfernen von Cascade Delete hinzugefügt und trotzdem bleibt der Fehler bestehen :( – Stuart

Antwort

1

Ich habe eine Lösung dafür gefunden, aber ich bin nicht sicher, warum es funktioniert oder ob es der richtige Weg ist. Wie auch immer,

_dbContext.Entry(selectedHoliday).State = EntityState.Deleted; 
_dbContext.SaveChanges();  

Und das Element wird gelöscht. Ich habe keine Ahnung, warum ich das tun muss und es nicht einfach von seinem Elternteil lösche, aber es funktioniert.

Verwandte Themen