locked
Code first foreign key woes RRS feed

  • Question

  • Could someone be so kind as to tell me what is wrong with the simple LunchDate class.

    The framework is generating a table with an extra column named 'Host_PersonId.

     public class LunchDate
        {
            public int LunchDateId { get; set;}
            public DateTime theDate { get; set; }
            public int HostId { get; set; }
            public virtual Person Host { get; set; }
        }

        public class Person
        {
            public int PersonId { get; set; }
            public string Name { get; set; }
        }
    Friday, August 5, 2011 8:41 PM

Answers

  • Hi,

    You should use the ForeignKeyAttribute to indicate that the HostId is the foreign key for Host property.
    The ForeignKeyAttribute takes a string as a parameter which is the name of the associated navigation property or the name of the associated foreign key.
    Here is an example:
     public class LunchDate
    {
        public int LunchDateId { get; set;}
        public DateTime theDate { get; set; }       
        public int HostId { get; set; }

        [ForeignKey("HostId")]
        public virtual Person Host { get; set; }
    }

    Another option will be to use the Code First Fleunt API in order to configure the same thing. You will override the OnModelCreating method in the DbContext and supply the relevant configurations.
    Gil Fink
    • Marked as answer by stocksp Saturday, August 6, 2011 9:49 PM
    Saturday, August 6, 2011 10:27 AM

All replies

  • Hi,

    You should use the ForeignKeyAttribute to indicate that the HostId is the foreign key for Host property.
    The ForeignKeyAttribute takes a string as a parameter which is the name of the associated navigation property or the name of the associated foreign key.
    Here is an example:
     public class LunchDate
    {
        public int LunchDateId { get; set;}
        public DateTime theDate { get; set; }       
        public int HostId { get; set; }

        [ForeignKey("HostId")]
        public virtual Person Host { get; set; }
    }

    Another option will be to use the Code First Fleunt API in order to configure the same thing. You will override the OnModelCreating method in the DbContext and supply the relevant configurations.
    Gil Fink
    • Marked as answer by stocksp Saturday, August 6, 2011 9:49 PM
    Saturday, August 6, 2011 10:27 AM
  • Hi stocksp,

    Welcome!

    About Fluent API you can refer this blog:http://blogs.msdn.com/b/adonet/archive/2010/12/06/ef-feature-ctp5-fluent-api-samples.aspx

    modelBuilder.Entity<LunchDate>() 
      .HasRequired(l => l.Host ) 
      .WithMany() 
      .HasForeignKey(l => l.HostId );
    

    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.

    Monday, August 8, 2011 6:16 AM