locked
foreign keys in models RRS feed

  • Question

  • User1924272441 posted

    hi , im working on mvc project , i got two models,Courses and prerequisite.

     public class Course
        {
            public int CoruseId { get; set; }
            public string Name { get; set; }
            public virtual Prerequisite prerequisite { get; set; }
        }
        public class Prerequisite 
        {
            public int prerequisiteId { get; set; }
            public int CoruseId { get; set; }
            public virtual ICollection<Course> Course{ get; set; }
    
        }

    Course: courseId ,Name -----this table will define courses.

    Prerequisite :

    prerequisiteId ---- is primary key and foreign key to coursid in course table

    courseId ---- is foreign key to coursid in course table

    i know this hard to explain without picture, sorry ..

    the problem here is how would i do this relationships in mvc module, how can i make prerequisiteId a foreign key and primary key in same time 

    Wednesday, September 7, 2011 9:17 PM

Answers

  • User-1942416292 posted
     public class Course
        {
            public int CourseId { get; set; }
            public string Name { get; set; }
    
            public virtual ICollection<Prerequisite> Course { get; set; }
    
        }
        public class Prerequisite 
        {
            public int prerequisiteId { get; set; }
    
            [ForeignKey("CourseId")]
            public Course Course { get; set; }
            public int CourseId { get; set; }
    
    
        }

    Hi One Course has Many Prerequisite , and in Prerequisite you have to define a foreignkey attribute, "CourseId" in Prerequisite is the property CourseId.

    Hope this help !




    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, September 7, 2011 9:33 PM
  • User-1942416292 posted

    Hi in your action Create

    you have to define something like this.

    private void PopulateCategories(object selected = null)

    {

    var categories = galleryRepository.FindAllCategories();
    ViewBag.PhotoCategoryId = new SelectList(categories,"CategoryId","CategoryName",selected);

    }

    then your View will be

    @Html.DropDownListFor(m => m.PhotoCategoryId,null)

    Remember

    ViewBag.PhotoCategoryId and your Model must have the same name m.PhotoCategoryId
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, September 8, 2011 2:28 AM

All replies

  • User-1942416292 posted
     public class Course
        {
            public int CourseId { get; set; }
            public string Name { get; set; }
    
            public virtual ICollection<Prerequisite> Course { get; set; }
    
        }
        public class Prerequisite 
        {
            public int prerequisiteId { get; set; }
    
            [ForeignKey("CourseId")]
            public Course Course { get; set; }
            public int CourseId { get; set; }
    
    
        }

    Hi One Course has Many Prerequisite , and in Prerequisite you have to define a foreignkey attribute, "CourseId" in Prerequisite is the property CourseId.

    Hope this help !




    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, September 7, 2011 9:33 PM
  • User1924272441 posted

    thanx but when creating the views it only show one dropdown list ,

    i need to choose from two dropdown list  the course itself and choose the prerequisite

    Thursday, September 8, 2011 12:19 AM
  • User-1942416292 posted

    Hi in your action Create

    you have to define something like this.

    private void PopulateCategories(object selected = null)

    {

    var categories = galleryRepository.FindAllCategories();
    ViewBag.PhotoCategoryId = new SelectList(categories,"CategoryId","CategoryName",selected);

    }

    then your View will be

    @Html.DropDownListFor(m => m.PhotoCategoryId,null)

    Remember

    ViewBag.PhotoCategoryId and your Model must have the same name m.PhotoCategoryId
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, September 8, 2011 2:28 AM
  • User3866881 posted

    Hello, Since you only have two tables and your One Prerequisite has more Course. So nothing has relations with Prerequisite, and I'm afraid you cannot do this accroding to your current situation.

    However in my mind, If you have another table. Maybe you can try this:

    public class Course
    {
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int CourseId { get; set; }
    public string Name { get; set; }
    public virtual Prerequisite prerequisite { get; set; }
    }
    public class Prerequisite
    {
    [Key, ForeignKey("Teacher")]
    public int PrerequisiteId { get; set; }
    public string PrerequisiteName { get; set; }
    public virtual ICollection<Course> Courses { get; set; }
    public virtual Teacher Teacher { get; set; }
    }
    public class Teacher
    {
    public int TeacherId { get; set; }
    public string TeacherName { get; set; }
    public virtual Prerequisite Prerequisite { get; set; }
    }

    public class DBCreator:DbContext
    {
    public DbSet<Teacher> Teachers { get; set; }
    public DbSet<Prerequisite> Prerequisites { get; set; }
    public DbSet<Course> Courses { get; set; }
    }
    Friday, September 9, 2011 9:44 PM