我有以下模型:
Public MustInherit Class Epreuve
Public Enum EpreuveType
CourseStade = 1
ConcoursSansBarre = 2
ConcoursAvecBarre = 3
EpreuveMultiple = 4
EpreuveCombinee = 5
End Enum
Public Property EpGuid As Guid
Public Property FormatPerf As String
Public Property CodeAppel As String
Public Property Nom As String
Public Property NomReduit As String
Public Property Chrono As String
Public Property Vent As Boolean
Public Property Ajustement As Double
Public MustOverride ReadOnly Property TypeEpreuve As EpreuveType
End Class
Public MustInherit Class EpreuveStade
Inherits Epreuve
End Class
Public MustInherit Class EpreuveComposee
Inherits EpreuveStade
Implements ISousEpreuve
Public Property EpParentGuid As Guid Implements ISousEpreuve.EpParentGuid
Public Property SousEpreuves As ObservableCollection(Of EpreuveStade) Implements ISousEpreuve.SousEpreuves
End Class
Public Class EpreuveCombinee
Inherits EpreuveComposee
Public Property EpreuveParent As EpreuveComposee
Public Overrides ReadOnly Property TypeEpreuve As EpreuveType
Get
Return EpreuveType.EpreuveCombinee
End Get
End Property
End Class
EpreuveComposee和EpreuveStade有很多关系。这是我完成的流畅 API:
With modelBuilder.Entity(Of Epreuve)
.HasKey(Function(ep) ep.EpGuid)
.Property(Function(ep) ep.EpGuid).HasDatabaseGeneratedOption(ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity)
.Map(Of EpreuveCombinee)(Function(f) f.Requires("TypeEpreuve").HasValue(DirectCast(Epreuve.EpreuveType.EpreuveCombinee, Integer)))
End With
With modelBuilder.Entity(Of EpreuveCombinee)
.HasRequired(Function(f) f.EpreuveParent).WithMany.HasForeignKey(Function(f) f.EpParentGuid)
End With
当我尝试添加迁移时,出现以下错误:
外键组件"EpParentGuid"不是 上的声明属性 键入"EpreuveCombinee"。验证它是否未显式 从模型中排除,并且它是有效的基元属性。
我已经找到了。
Public MustInherit Class EpreuveComposee
Inherits EpreuveStade
Implements ISousEpreuve
Public Property EpParentGuid As Guid Implements ISousEpreuve.EpParentGuid
Public Property EpParent As Epreuve Implements ISousEpreuve.EpParent
Public Property SousEpreuves As ObservableCollection(Of Epreuve) Implements ISousEpreuve.SousEpreuves
End Class
Public Class EpreuveCombinee
Inherits EpreuveComposee
End Class
和流畅的 API :
With modelBuilder.Entity(Of EpreuveComposee)
.HasMany(Function(f) f.SousEpreuves).WithMany().Map(Sub(m)
m.ToTable("EpreuveSousEpreuves")
m.MapLeftKey("EpGuid")
m.MapRightKey("EpParentGuid")
End Sub)
End With