我正在尝试将这些类与 EF Code-First 一起使用来将数据添加到数据库中。在我填充对象并尝试填充上下文“dbContext.Tracks.Add(t1)”之后。我收到错误“无法将'System.Collections.Generic.HashSet`1 [Vinyl7]'类型的对象转换为'Vinyl7'”有人能指出我遗漏了什么吗?
Public Class Vinyl7
<Key>
Public Overridable Property mediaId As Long
Public Overridable Property parentMediaId As System.Nullable(Of Long)
Public Overridable Property side As String
<ForeignKey("mediaId"), Required>
Public Overridable Property track As Track
End Class
Public Class Track
<Key>
Public Property mediaId As Long
Public Property title As String
Public Overridable Property Vinyl7T As ICollection(Of Vinyl7) = New HashSet(Of Vinyl7)
End Class
这是我的上下文
Public Class MediaRepo : Inherits DbContext
Public Sub New()
MyBase.New("name=MediaDB")
End Sub
Protected Overrides Sub OnModelCreating(modelBuilder As DbModelBuilder)
End Sub
Public Property Vinyl7s As DbSet(Of Vinyl7)
Public Property Tracks As DbSet(Of Track)
End Class
这是我的方法。
Using db = New MediaRepo
Dim t1 = New Track
t1.title = "Kutchie"
Dim t2 = New Track
t2.title = "Drug Trade"
Dim a = New Media.Vinyl7
a.side = "A"
a.parentMediaId = t1.mediaId
a.track = t1
Dim b = New Media.Vinyl7
b.side = "B"
b.parentMediaId = t1.mediaId
b.track = t2
t1.Vinyl7T.Add(a)
t1.Vinyl7T.Add(b)
db.Tracks.Add(t1)
SaveChanges(db)
End Using