EF:on delete对可空外键不起作用的操作

时间:2017-11-24 15:00:50

标签: entity-framework mariadb sql-delete

以下情景:

我有一个实体'材料'使用实体所需的外键' MaterialType'。我与ON DELETE NO ACTION建立了关系。 如果我尝试删除材料中使用的MaterialType,mariaDB将拒绝声明此MaterialType仍在使用中的声明,因此无法删除。这是有效的,是预期的行为。到目前为止一切顺利。
实体材料还有一个可选的外键,用于添加权利'添加剂。这种关系也有ON DELETE NO ACTION。如果我这次尝试删除Additive,mariaDB数据库会按预期拒绝该语句。但EF会将对材料中添加剂的所有引用设置为NULL,并最终删除添加剂 我在两个外键上都禁用了级联删除。我在这里错过了什么?或者我真的必须在删除实体之前自己检查所有可空的关系?

这是材料:

Public partial class Material 
       Inherits Entity

        ''' <summary>
        ''' Additive that enhances this material.
        ''' </summary>
        <System.ComponentModel.DisplayName("Verstärkung")> _
        Public Overridable Property AdditiveId() As Global.System.Nullable(Of Long)
            Get
                Return _AdditiveId
            End Get
            Set
                If (Object.Equals(_AdditiveId, value) = false) Then
                  Me.OnAdditiveIdChanging(value)
                  OnPropertyChanging("AdditiveId")
                  _AdditiveId = value
                  Me.OnAdditiveIdChanged()
                  OnPropertyChanged("AdditiveId")
                End If
            End Set
        End Property
        Private _AdditiveId As Global.System.Nullable(Of Long)

        ''' <summary>
        ''' The material type of this material, e.g. PC, PET, etc.
        ''' </summary>
        <System.ComponentModel.DataAnnotations.Required()> _
        <System.ComponentModel.DisplayName("Materialtyp")> _
        Public Overridable Property MaterialTypeId() As Long
            Get
                Return _MaterialTypeId
            End Get
            Set
                If (Object.Equals(_MaterialTypeId, value) = false) Then
                  Me.OnMaterialTypeIdChanging(value)
                  OnPropertyChanging("MaterialTypeId")
                  _MaterialTypeId = value
                  Me.OnMaterialTypeIdChanged()
                  OnPropertyChanged("MaterialTypeId")
                End If
            End Set
        End Property
        Private _MaterialTypeId As Long

        ''' <summary>
        ''' The material type of this material.
        ''' </summary>
        <System.ComponentModel.DisplayName("Materialtyp")> _
        Public Overridable Property MaterialType() As MaterialType

        ''' <summary>
        ''' The additive that enhances this material, if any.
        ''' </summary>
        <System.ComponentModel.DisplayName("Verstärkung")> _
        Public Overridable Property Additive() As Additive
End Class

这是我的模特:

        Partial Public Class Data
           Inherits DbContext

           Protected Overrides Sub OnModelCreating(modelBuilder As DbModelBuilder)
              ' Material

               modelBuilder.Entity(Of Material)() _
                   .Map(Sub(tpc)
                         tpc.MapInheritedProperties()
                         tpc.ToTable("Materials")
                     End Sub)
               ' Properties:
               modelBuilder.Entity(Of Material)() _
                   .Property(Function(p) p.AdditiveContent) _
                       .HasColumnType("double")
               modelBuilder.Entity(Of Material)() _
                .Property(Function(p) p.AdditiveId) _
                    .HasColumnType("bigint")
               modelBuilder.Entity(Of Material)() _
                   .Property(Function(p) p.ManufacturerId) _
                    .IsRequired() _
                    .HasColumnType("bigint")
            modelBuilder.Entity(Of Material)() _
                .Property(Function(p) p.MaterialTypeId) _
                    .IsRequired() _
                    .HasColumnType("bigint")
            ' Associations:
            modelBuilder.Entity(Of Material)() _
                .HasMany(Function(p) p.Tasks) _
                    .WithRequired(Function(c) c.Material) _
                .HasForeignKey(Function(p) p.MaterialId) _
                    .WillCascadeOnDelete(False)

            ' Additive

            modelBuilder.Entity(Of Additive)() _
                .Map(Sub(tpc)
                         tpc.MapInheritedProperties()
                         tpc.ToTable("Additives")
                     End Sub)
            ' Association:
            modelBuilder.Entity(Of Additive)() _
                .HasMany(Function(p) p.Materials) _
                    .WithOptional(Function(c) c.Additive) _
                .HasForeignKey(Function(p) p.AdditiveId) _
                    .WillCascadeOnDelete(False)
   End Sub
End Class

这就是我删除的方式:

 If RadMessageBox.Show(String.Format("{0} wirklich löschen?", msg), "Löschen", MessageBoxButtons.YesNo) = DialogResult.Yes Then
        For Each row In e.Rows
            Data.Entry(row.DataBoundItem).State = EntityState.Deleted
        Next
        Try
            Data.SaveChanges()
        Catch ex As Exception
            Util.printError(ex)
            e.Cancel = True
            reload()
        End Try
    End If

0 个答案:

没有答案