none
EF 4.2 Code First - Table relationship not created RRS feed

  • Question

  • Hello
    I am using EF4.2/VB.NET/SQL Server Compact 4 and am having difficulty getting two related entity's one-to-many relationship correctly mapped to the database. When using the default convetions the foriegn-key and navigation properties appear in the db, but the foriegn-key is not marked as such and there is no relationsip. If I try and force the matter using the 'ForeignKey' annotation or the fluent api I get the following error...
    'The navigation property 'Associate' is not a declared property on type 'MembershipGroup'. Verify that it has not been explicitly excluded from the model and that it is a valid navigation property.'
     
    Does anyone have an idea why this is happening? And if so, how do I fix it? Do I need to make any changes to the entities?
    Associate Class:
    <Table("Associates")>
    Public Class Associate
        Implements IAssociate
        Implements ICommentator
    
    
        Public Overridable Property ID As Integer Implements IAssociate.Number
    
        Public Overridable Property Address As PostalAddress Implements IAssociate.Address
    
        Public Overridable Property Bank As PaymentMethod Implements IAssociate.Bank
    
        Public Overridable Property Fax As String Implements IAssociate.Fax
    
        Public Overridable Property Name As CompanyName Implements IAssociate.Name
    
        Public Overridable Property Owner As HumanName Implements IAssociate.Owner
    
        Public Overridable Property Telephone As String Implements IAssociate.Telephone
    
        Public Overridable Property Cancelled As Date? Implements IAssociate.Cancelled
    
        Public Overridable Property Registered As Date Implements IAssociate.Registered
    
        Public Overridable Property Email As String Implements IAssociate.Email
    
        Public Overridable Property Charges As AssociateCharges Implements IAssociate.Charges
    
        Public Overridable Property Comments As ICollection(Of Comment) Implements ICommentator.Comments
    
    End Class
    
    Membership Group Class:
    <Table("Groups")>
    Public Class MembershipGroup
        Implements IMembershipGroup
    
        Public Property ID As Integer Implements IMembershipGroup.Number
    
        Public Property Associate As IAssociate Implements IMembershipGroup.Associate
    
        Public Property Members As ICollection(Of IMember) Implements IMembershipGroup.Members
    
        Public Property Instructions As ICollection(Of IPaymentInstruction) Implements IMembershipGroup.Instructions
    
        Public ReadOnly Property Member(membershipNumber As Integer) As IMember Implements IMembershipGroup.Member
            Get
                'some code
            End Get
        End Property
    
        Public ReadOnly Property Instruction(instructionNumber As Integer) As IPaymentInstruction Implements IMembershipGroup.Instruction
            Get
                'some code
            End Get
        End Property
    
        Public ReadOnly Property Instruction(activeFrom As Date, activeUntil As Date) As IPaymentInstruction Implements IMembershipGroup.Instruction
    
            Get
               'some code
            End Get
        End Property
    
        <ForeignKey("Associate")>
        Public Property AssociateID As Integer
    
    End Class
    
     

    Here is a pic of the database afterwards...
     
     
     
     
    And to add more confusiom, the MembershipGroups entity (above) and the Members entity (below) map perfectly to the db.
     
     
    <Table("Members")>
    Public Class Member
        Implements IMember
        Implements ICommentator
    
        Public Overridable Property ID As Integer Implements IMember.Number
    
        Public Property Address As PostalAddress Implements IMember.Address
    
        Public Property Born As Date Implements IMember.Born
    
        Public Property Cancelled As Date? Implements IMember.Cancelled
    
        Public Property Email As String Implements IMember.Email
    
        Public Property Home As String Implements IMember.Home
    
        Public Property Loyalty As LoyaltyLevel Implements IMember.Loyalty
    
        Public Property Mobile As String Implements IMember.Mobile
    
        Public Property Name As HumanName Implements IMember.Name
    
        Public Property Registered As Date Implements IMember.Registered
    
        Public Property Scheme As MembershipScheme Implements IMember.Scheme
    
        Public Property Type As SCNType Implements IMember.Type
    
        Public Property Work As String Implements IMember.Work
    
        Public Property MembershipGroupID As Integer
    
        Public Property MembershipGroup As MembershipGroup
    
        Public Property Comments As ICollection(Of Comment) Implements ICommentator.Comments
    
    End Class
    
    .

    ding_jimmy




    • Edited by ding_jimmy Wednesday, November 16, 2011 5:53 PM
    Wednesday, November 16, 2011 5:37 PM

Answers

  • ahh okay, so that was the cause. Sorry to be the bearer of bad news, though. I will ask someone on the team if they know about this. And it's bad news for me since the Code First mapping book just got signed off for printing yesterday. I would love to have at least put a little warning note about this in there. I'll see if we can sneak it in at some point.

    Would it work for you to have a concrete type as the related property and an unmapped interface type to expose Associate to your code. You could make the concrete type have a private setter. Just an idea.


    Julie Lerman, Author of Programming Entity Framework, MVP
    • Edited by Julie LermanMVP Thursday, November 17, 2011 1:43 PM added 2nd par 30 seconds after entering the initial response
    • Proposed as answer by Alan_chenModerator Friday, November 18, 2011 7:57 AM
    • Marked as answer by ding_jimmy Tuesday, November 22, 2011 5:32 PM
    Thursday, November 17, 2011 1:38 PM
  • Hi ding,

    @Julie is right! Here is a link, hope it helps: http://antix.co.uk/Blog/EF4-Code-First-and-Interface-Property-Types

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by ding_jimmy Tuesday, November 22, 2011 5:32 PM
    Friday, November 18, 2011 7:59 AM
    Moderator

All replies

  • As a test, can you change the GroupMemberhip.Associate property to be an Associate type not IAssociate and see if anything changes? I've done a LOT with mapping relationships, especially having just co-authored a book solely on Code First mappings, and the only thing that looks suspicious is the interface. I wonder if that's what's bothering the modeler and if so, if there's a way around it. But I need first to know if that is, indeed, the problem.

    FWIW, the AssociateID foreign key follows naming conventions for FK, so adding the configuration (I'm seeing a Data Annotation, not a fluent api config) is not the answer. In fact, becasue of whatever the core problem is, Code FIrst got mad at your for trying to force it to make that a foreign key . :)

     


    Julie Lerman, Author of Programming Entity Framework, MVP
    Thursday, November 17, 2011 1:35 AM
  • Hi Julie and thank you for the response.

    I've tested replacing the 'IAssociate' interface with 'Associate' and it works. This could be a problem.... I don't want to change any of the interfaces if I can avoid it; they are included as part of a shared dll and used heavily in several applications.

    Any ideas?

     

    PS: As a weird co-incidence I ordered two of your books yesterday morning.


    ding_jimmy
    • Edited by ding_jimmy Thursday, November 17, 2011 12:18 PM
    Thursday, November 17, 2011 12:17 PM
  • ahh okay, so that was the cause. Sorry to be the bearer of bad news, though. I will ask someone on the team if they know about this. And it's bad news for me since the Code First mapping book just got signed off for printing yesterday. I would love to have at least put a little warning note about this in there. I'll see if we can sneak it in at some point.

    Would it work for you to have a concrete type as the related property and an unmapped interface type to expose Associate to your code. You could make the concrete type have a private setter. Just an idea.


    Julie Lerman, Author of Programming Entity Framework, MVP
    • Edited by Julie LermanMVP Thursday, November 17, 2011 1:43 PM added 2nd par 30 seconds after entering the initial response
    • Proposed as answer by Alan_chenModerator Friday, November 18, 2011 7:57 AM
    • Marked as answer by ding_jimmy Tuesday, November 22, 2011 5:32 PM
    Thursday, November 17, 2011 1:38 PM
  • Hi ding,

    @Julie is right! Here is a link, hope it helps: http://antix.co.uk/Blog/EF4-Code-First-and-Interface-Property-Types

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by ding_jimmy Tuesday, November 22, 2011 5:32 PM
    Friday, November 18, 2011 7:59 AM
    Moderator
  • Hi

    @Alen_chen & @Jule - Thanks for the tips. I was thinking along the same lines and have successfully implemented this!

     

    Again, many thanks!


    ding_jimmy
    • Edited by ding_jimmy Tuesday, November 22, 2011 5:33 PM
    Tuesday, November 22, 2011 5:32 PM