1
我正在使用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
爲什麼難道你不只是[谷歌](http://www.entityframeworktutorial.net/code-first /foreignkey-dataannotations-attribute-in-code-first.aspx)問題的標題?你會發現很多關於數據註釋和約定的材料。 –