实体框架代码首先 - 外键关系

时间:2017-02-14 09:04:00

标签: asp.net-mvc vb.net ef-code-first

我使用ASP.NET MVC身份并尝试使用代码优先方法创建其他表。例如,我的主表名为Orders,它被引用回AspNetUsers表(id),并且可以存储每个用户的多个订单。主表中的单独ID将链接到辅助表中的其他订单详细信息。

我正在努力实现两个表之间的关系?如果有人可以请求帮助,谢谢

Public Class ApplicationUser
Inherits IdentityUser
    Public Overridable Property Orders() As Orders
End Class

Public Class Orders
   Public Property Id As String   'Should link to Id in AspNetUsers Table
   Public Property TimeandDate As Datetime
   Public Property OrderNo As Integer
   Public Overridable Property OrderLineDetail() As ICollection(Of OrderLineDetail)
End Class

Public Class OrderLineDetail
   Public Property Description As String
   Public Property Price As Decimal
End class

Public Class ApplicationDbContext
    Inherits IdentityDbContext(Of ApplicationUser)

    Public Sub New()
         MyBase.New("DefaultConnection", throwIfV1Schema:=False)
    End Sub

    Public Property Orders() As DbSet(Of Orders)

    Public Shared Function Create() As ApplicationDbContext
        Return New ApplicationDbContext()
    End Function

End Class

1 个答案:

答案 0 :(得分:1)

在你的AspNetUsers表中试试这个:

  Public Property Orders As ICollection(Of Orders)
        Get
        End Get
        Set
        End Set
    End Property