none
How do i create Foreign Key relationship in .netframework 3.5/Code First Model? RRS feed

  • Question

  • Using System.ComponentModel.DataAnnotations;
    namespace xyz {
        [DataContract]
        public class Category
        {       
            public string category_id { get; set; }  
    
            public string category_name { get; set; }    
    
            public virtual ICollection<SubCategory> SubCategorys { get; set; }
        }
    
    
        [DataContract]
    
        public class SubCategory 
        {
            public string sub_category_id { get; set; }
            public string sub_category_name { get; set; }
            public virtual ICollection<SubCategory2> SubCategory2s { get; set; }
            [ForeignKey("category_id")]
            public virtual Category Category { get; set; }
    
        }
    
        [DataContract]
        public class SubCategory2 
        {
        public string sub_category2_id { get; set; }
        public string sub_category2_name { get; set; }
        [ForeignKey("sub_category_id")]
        public virtual SubCategory SubCategory { get; set; }
        }
    }

    Friday, June 3, 2016 5:50 PM

Answers

  • Hi DotNetLover2020,

    Based on your code snippet, I create a code first project, please modify your code like this:

    #Models:

        public class Category
        {
            [Key]
            public string category_id { get; set; }
    
            public string category_name { get; set; }
    
            public virtual ICollection<SubCategory> SubCategorys { get; set; }
        }
    
    
       
    
        public class SubCategory
        {
            [Key]
            public string sub_category_id { get; set; }
            public string sub_category_name { get; set; }
            public string category_id { get; set; }
            public virtual ICollection<SubCategory2> SubCategory2s { get; set; }
            [ForeignKey("category_id")]
            public virtual Category Category { get; set; }
    
        }
    
       
        public class SubCategory2
        {
            [Key]
            public string sub_category2_id { get; set; }
            public string sub_category_id { get; set; }
            public string sub_category2_name { get; set; }
            [ForeignKey("sub_category_id")]
            public virtual SubCategory SubCategory { get; set; }
        }
    

    #DbContext

    public partial class CategoryDbContext: DbContext
        {
            public CategoryDbContext()
                : base("name=Category")
            {
            }
    
            public DbSet<Category> Categories { get; set; }
    
            public DbSet<SubCategory> SubCategories { get; set; }
    
            public DbSet<SubCategory2> SubCategory2s { get; set; }
    
            protected override void OnModelCreating(DbModelBuilder modelBuilder)
            {
            }
        }

    For more information about code first approach, please refer to:

    https://msdn.microsoft.com/en-sg/data/jj193542

    Best regards,

    Cole Wu


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, June 6, 2016 9:43 AM
    Moderator

All replies

  • What does this have to do with WCF? Below is where you need to post.

    http://social.msdn.microsoft.com/Forums/en-US/home?forum=adodotnetentityframework

    Friday, June 3, 2016 7:59 PM
  • Hi DotNetLover2020,

    Based on your code snippet, I create a code first project, please modify your code like this:

    #Models:

        public class Category
        {
            [Key]
            public string category_id { get; set; }
    
            public string category_name { get; set; }
    
            public virtual ICollection<SubCategory> SubCategorys { get; set; }
        }
    
    
       
    
        public class SubCategory
        {
            [Key]
            public string sub_category_id { get; set; }
            public string sub_category_name { get; set; }
            public string category_id { get; set; }
            public virtual ICollection<SubCategory2> SubCategory2s { get; set; }
            [ForeignKey("category_id")]
            public virtual Category Category { get; set; }
    
        }
    
       
        public class SubCategory2
        {
            [Key]
            public string sub_category2_id { get; set; }
            public string sub_category_id { get; set; }
            public string sub_category2_name { get; set; }
            [ForeignKey("sub_category_id")]
            public virtual SubCategory SubCategory { get; set; }
        }
    

    #DbContext

    public partial class CategoryDbContext: DbContext
        {
            public CategoryDbContext()
                : base("name=Category")
            {
            }
    
            public DbSet<Category> Categories { get; set; }
    
            public DbSet<SubCategory> SubCategories { get; set; }
    
            public DbSet<SubCategory2> SubCategory2s { get; set; }
    
            protected override void OnModelCreating(DbModelBuilder modelBuilder)
            {
            }
        }

    For more information about code first approach, please refer to:

    https://msdn.microsoft.com/en-sg/data/jj193542

    Best regards,

    Cole Wu


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, June 6, 2016 9:43 AM
    Moderator