1

I've read How Should I Declare Foreign Key Relationships Using Code First Entity Framework (4.1) in MVC3? but I can't get my call to yield any results. It has the following error:

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.

On this line:

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

Here is my parent model:

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

Here is my child model:

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

What can I do to make the action yield a result without error? Thanks.

Community
  • 1
  • 1
user1477388
  • 20,790
  • 32
  • 144
  • 264

1 Answers1

6

Your foreign key is annotation should be for the name of the property, not the type.

Your navigation property is Header:

Public Overridable Property Header As Tbl_Footer_Header

So your annotation, should reference the property. Change it to:

<ForeignKey("Header")>
Public Property Footer_Item_Header_ID() As Integer
Mark Oreta
  • 10,346
  • 1
  • 33
  • 36
  • Thanks, but I now get an error that says, "`Invalid column name 'Footer_Header_Order'`." There is clearly a Footer_Header_Order column and property, so what gives? Thanks. – user1477388 Sep 04 '12 at 16:34
  • I see an integer representing the order: Public Property Footer_Header_Order() As Integer, is that what you intended, or should that be the ID? – Mark Oreta Sep 04 '12 at 16:36
  • It should be an integer since I will be using it to determine the order of items within the list i.e. 1, 2, 3 (all integers). – user1477388 Sep 04 '12 at 16:38
  • There was a `Public Property Footer_Header_Order() As Integer` line in the child model which shouldn't have been there. That is the one the error was referring to (not the one in the parent model as I had suspected). Thanks for your help :) – user1477388 Sep 04 '12 at 16:57
  • not only did this help me understand writing entities from scratch, it also helped me refresh (or at least re-discover) VB.NET. – AceMark Sep 08 '13 at 14:51