2017-03-29 12 views
1

コードネームのEFデータベースソリューションがあります。以下は私のモデルクラスです:エンティティフレームワーク、コレクションから削除すると外部キー例外が発生する

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; } 
} 

マイ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; } 
} 

holidayAllowanceから休日を削除しようとする私のDataServiceから方法:

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

    allowance? 
     .Holidays 
     .Remove(selectedHoliday); 

    _dbContext.SaveChanges(); 
} 

私の方法それを呼び出すViewModel:

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)); 
} 

問題は、私は次のエラーを取得するHolidayAllowance.HolidaysコレクションからHolidayを削除しようとすると、次のとおりです。

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.

ことがFK違反だろう、なぜ私は理解していませんか?私はコレクションから削除しようとしています。

EFが生成しているMy DBテーブル。

enter image description here

私は、次のSQLを実行することができ、それがFK例外をスローせずに動作します。

delete from dbo.Holidays 
where Id = 2 

IスクリプトはSSMSで

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 

を作成する場合は外部キーの生成は、私は@Falsは、下記のコメント読んだ後、私のDBContextに次のように試してみた、このようになります。

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

私はEFも同様のことをしていると思いますか?まだ運がありません。

+0

カスケードが有効になっているかどうかを確認します。他の依存関係も一緒に削除しようとしている可能性があります。 – Fals

+0

@Fals、私は質問を更新しました。削除カスケード削除規則を追加しましたが、それでもエラーは解決しません。 – Stuart

答えて

1

私はこれに対する修正を見つけましたが、なぜそれがうまくいくのか、それが正しい方法であるかはわかりません。とにかく、

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

そして項目が削除されます。なぜ私はこれをやらなければならないのか分かりませんし、親から削除するだけではありませんが、うまくいきます。