0

suppose i have table student

public class Student
{
    public int Id { get; set; }      
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Class { get; set; }
    public string ParentName { get; set; }
    public string Address { get; set; }
    public int CourseId { get; set; }
    public virtual Course Course { get; set; }
}

and another table Course

public class Course
{
    public int Id { get; set; }
    public string Name { get; set; }
}

from above two model class we can there is a relationship course with courseId any foreign key for those table is it possible by using mvc entity framework i used uniformity for this i got "Invalid column name 'Course_Id'" error by using this is there any possible to give relationship to those table can any one help me.

Meena
  • 169
  • 1
  • 9
  • Which framework are you using? Also, right off the bat, if you already have a Course in your Student, then your course's ID should be accessed through the Course, not with an extra courseId in your student (specially because there will probably be multiple courses per student) – Rahul Bhatnagar May 18 '17 at 06:30
  • i am using ef (6.0.0.0) (specially because there will probably be multiple courses per student) i created for sample purpose is there any possible way for this – Meena May 18 '17 at 06:34

1 Answers1

1

What you're looking for is a many-to-many relationship between courses and students. The exact same example is given at : http://www.entityframeworktutorial.net/entity-relationships.aspx

Since entities have to be related to one another in some format or the other, you will have to declare some foreign key in Courses as well.

A similar question and its response can be found here, check it out as well : Entity Framework Code first mapping without foreign key

Community
  • 1
  • 1