none
Mapping problem with FK to own table (Code First) - EF 4.2 RRS feed

  • Question

  • Hi.

    I'm becoming crazy with it.

    I have in my code first classes a FK to the own object thru property MenuOptionParent_IdMenuOption. 

     

    namespace SGA.Entity
    {
        using System;
        using System.Collections.Generic;
        
        public partial class MenuOption
        {
            public MenuOption()
            {
                this.MenuOptionChildren = new HashSet<MenuOption>();
                this.Profile = new HashSet<Profile>();
            }
        
            public string IdMenuOption { getset; }
            public string Description { getset; }
            public string MenuOptionParent_IdMenuOption { getset; }
            public string Url { getset; }
        
            public virtual ICollection<MenuOption> MenuOptionChildren { getset; }
            public virtual MenuOption MenuOptionParent { getset; }
            public virtual ICollection<Profile> Profile { getset; }
        }
    }

     

    And my database look like this:

    The problem is all my tries to query over "MenuOption" always fail as I get error "Invalid column name 'MenuOptionParent_IdMenuOption1'.".

    I have tried with some alternatives with out results:

    1. Map the Fields thru fluent API:

     

    modelBuilder.Entity<MenuOption>().Property(x => x.MenuOptionParent_IdMenuOption).HasColumnName("MenuOptionParent_IdMenuOption")

     

    Error: "Invalid column name 'MenuOptionParent_IdMenuOption1'."

    2. Explicit FK definition:

    modelBuilder.Entity<MenuOption>().HasOptional(m => m.MenuOptionParent).WithMany(pm => pm.MenuOptionChildren).Map(m => m.MapKey("MenuOptionParent_IdMenuOption"));
    

    Error: The property name 'MenuOptionParent_IdMenuOption' is already defined.

    3. Rename dabase field to "MenuOptionParent_IdMenuOption1"

    Error: "Invalid column name 'MenuOptionParent_IdMenuOption'."

    Any suggestion will be welcome !!!

     

     


    Andres Duque
    Saturday, October 29, 2011 10:05 PM

Answers

  • Hi,

    You should be changing your class definition like

     

    namespace SGA.Entity
    {
        using System;
        using System.Collections.Generic;
        
        public partial class MenuOption
        {
            public MenuOption()
            {
                this.MenuOptionChildren = new HashSet<MenuOption>();
                this.Profile = new HashSet<Profile>();
            }
    
    
            [Key]  
            public long Id {get;set;} //Assuming this as a key
            public string IdMenuOption { get; set; }
            public string Description { get; set; }
           // public string MenuOptionParent_IdMenuOption { get; set; }
            public string Url { get; set; }
        
            public virtual ICollection<MenuOption> MenuOptionChildren { get; set; }
            public virtual MenuOption MenuOptionParent { get; set; }
     
            public virtual long MenuOptionParentId{get; set;}
    
            public virtual ICollection<Profile> Profile { get; set; }
        }
    }
     
    


     


    If this post answers your question, please click "Mark As Answer". If this post is helpful please click "Mark as Helpful".
    • Edited by Kris444 Tuesday, November 1, 2011 8:41 AM
    • Marked as answer by Alan_chenModerator Tuesday, November 8, 2011 6:51 AM
    Tuesday, November 1, 2011 8:41 AM