2014-07-16 80 views
0

我以前用EF Code First From Database生成基於現有數據庫的類。我需要映射這兩個類之間的關係。我懷疑我必須設置外鍵,但不要設置外鍵。如何映射這兩個生成的類之間的關係?

Partial Public Class be_Posts 
    <Key> 
    Public Property PostRowID As Integer 
    Public Property BlogID As Guid 
    Public Property PostID As Guid 
    <StringLength(255)> 
    Public Property Title As String 
    Public Property Description As String 
    Public Property PostContent As String 
    Public Property DateCreated As Date? 
    Public Property DateModified As Date? 
    <StringLength(50)> 
    Public Property Author As String 
    Public Property IsPublished As Boolean? 
    Public Property IsCommentEnabled As Boolean? 
    Public Property Raters As Integer? 
    Public Property Rating As Single? 
    <StringLength(255)> 
    Public Property Slug As String 
    Public Property IsDeleted As Boolean 
End Class 



Partial Public Class be_PostTag 
    <Key> 
    Public Property PostTagID As Integer 
    Public Property BlogID As Guid 
    Public Property PostID As Guid 
    Public Property PostRowID As Integer 
    <StringLength(50)> 
    Public Property Tag As String 
End Class 

回答

0

看起來很簡單,但我可能會錯過一些東西。有一段時間沒有在VB編碼,所以原諒任何錯別字。

您需要在兩個表的PostRowID值上添加一個外鍵關係。

我相信你完成後應該看起來像這樣。

Partial Public Class be_Posts 
    <Key> 
    Public Property PostRowID As Integer 
    Public Property BlogID As Guid 
    Public Property PostID As Guid 
    <StringLength(255)> 
    Public Property Title As String 
    Public Property Description As String 
    Public Property PostContent As String 
    Public Property DateCreated As Date? 
    Public Property DateModified As Date? 
    <StringLength(50)> 
    Public Property Author As String 
    Public Property IsPublished As Boolean? 
    Public Property IsCommentEnabled As Boolean? 
    Public Property Raters As Integer? 
    Public Property Rating As Single? 
    <StringLength(255)> 
    Public Property Slug As String 
    Public Property IsDeleted As Boolean 
End Class 



Partial Public Class be_PostTag 
    <Key> 
    Public Property PostTagID As Integer 
    Public Property BlogID As Guid 
    Public Property PostID As Guid 
    Public Property PostRowID As Integer 
    <StringLength(50)> 
    Public Property Tag As String 
    <ForeignKey("PostRowID")> 
    Public Property PostRow As be_Posts 
End Class 

下面是引用定義與代碼首先這個FK關係的一個簡單的例子的文章,該例子是在C#中,但應該翻譯得相當好。

+0

謝謝你的回答。但它沒有工作。我將 Public Property PostRow添加爲be_Posts爲be_PostTag,並在be_Posts中將添加到PostRowID。然後拋出錯誤該屬性不能配置爲導航屬性。該屬性必須是有效的實體類型。 –

+0

@AshokPadmanabhan這裏有一篇類似的文章,其答案就是提供了很好的信息和例子,你能否看到這些例子是否爲你的新問題提供了答案。再次抱歉,這些例子是在C#中。 http://stackoverflow.com/questions/20202578/relationships-in-entity-framework-code-first – Madullah

相關問題