无法转换'System.Collections.Generic.HashSet类型的对象

时间:2013-03-07 03:27:08

标签: entity-framework ef-code-first code-first

我正在尝试将这些类与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

1 个答案:

答案 0 :(得分:0)

我认为你的注释是错误的。您引用mediaId作为Track的外键,但这是Vinyl7实体的主键。您可能意味着parentMediaId对吗?

使用此更新您的Vinyl7实体,它将起作用:

   <Key>
    Public Overridable Property mediaId As Long       
    Public Overridable Property side As String

    <ForeignKey("mediaId"), Required>
    Public Overridable Property track As Track

修改

根据你的评论,我已经更新为1..0到1的关系。

制作了一款快速测试应用程序,你可以将它与你的产品进行比较吗?也许你有额外的东西让EF感到困惑?

 Public Class Vinyl7

    <Key>
    Public Overridable Property mediaId As Long
    Public Overridable Property side As String

    <ForeignKey("mediaId"), Required>
    Public Overridable Property track As Track

End Class

Public Class Track
    <Key>
    Public Property trackid As Long
    Public Property title As String

    Public Overridable Property Vinyl7T As Vinyl7

End Class

Module Module1

    Public Class MediaRepo : Inherits DbContext

        Public Sub New()
        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

    Sub Main()
        Dim db As New MediaRepo

        Dim vin = New Vinyl7
        Dim track As New Track

        track.Vinyl7T = vin

        db.Tracks.Add(track)
        db.SaveChanges()

    End Sub

End Module