2012-09-04 104 views
1

我讀過How Should I Declare Foreign Key Relationships Using Code First Entity Framework (4.1) in MVC3?但我無法接到我的電話以產生任何結果。它具有以下錯誤:屬性上的ForeignKeyAttribute無效

The ForeignKeyAttribute on property 'Footer_Item_Header_ID' on type 'MyBlog.Tbl_Footer_Item' is not valid. The navigation property 'Tbl_Footer_Header' was not found on the dependent type 'MyBlog.Tbl_Footer_Item'. The Name value should be a valid navigation property name.

在此行中:

Dim footerNavElements = db.Tbl_Footer_Headers.Where(Function(i) i.Footer_Header_Order = 1).Single.Items 

這裏是我的父模型:

Imports System.Data.Entity 
Imports System.ComponentModel.DataAnnotations 

Public Class Tbl_Footer_Header 

    <Key()> Public Property Footer_Header_ID() As Integer 
    Public Property Footer_Header_Content() As String 
    Public Property Footer_Header_Order() As Integer 

    Public Overridable Property Items As ICollection(Of Tbl_Footer_Item) 

End Class 

Public Class FooterHeaderDbContext 
    Inherits DbContext 

    Public Property Tbl_Footer_Headers As DbSet(Of Tbl_Footer_Header) 

End Class 

這裏是我的孩子型號:

Imports System.Data.Entity 
Imports System.ComponentModel.DataAnnotations 

Public Class Tbl_Footer_Item 

    <Key()> Public Property Footer_Item_ID() As Integer 
    <ForeignKey("Tbl_Footer_Header")> 
    Public Property Footer_Item_Header_ID() As Integer 
    Public Property Footer_Item_Content() As String 
    Public Property Footer_Item_Link() As String 
    Public Property Footer_Header_Order() As Integer 

    Public Overridable Property Header As Tbl_Footer_Header 

End Class 

Public Class FooterItemDbContext 
    Inherits DbContext 

    Public Property Tbl_Footer_Items As DbSet(Of Tbl_Footer_Item) 
    Public Property Tbl_Footer_Headers As DbSet(Of Tbl_Footer_Header) 

End Class 

我能做些什麼來使行動產生結果w沒有錯誤?謝謝。

回答

5

你的外鍵是註釋應該是屬性的名稱,而不是類型。

您的導航屬性是頁眉:

Public Overridable Property Header As Tbl_Footer_Header 

所以,你的註釋,應該引用屬性。更改爲:

<ForeignKey("Header")> 
Public Property Footer_Item_Header_ID() As Integer 
+0

謝謝,但我現在得到一個錯誤,說:「無效的列名'Footer_Header_Order''。」。顯然有一個Footer_Header_Order列和屬性,所以給了什麼?謝謝。 – user1477388

+0

我看到一個整數代表的順序:Public Property Footer_Header_Order()As Integer,是你想要的,或者應該是ID? –

+0

它應該是一個整數,因爲我將使用它來確定列表中的項目順序,即1,2,3(全部整數)。 – user1477388

相關問題