How to represent relation many to many in code first asp.net [Resolved]

Posted by Ahmedsa under ASP.NET MVC on 11/15/2016 | Points: 10 | Views : 175 | Status : [Member] | Replies : 1
Problem
I have two table Student and class many to many relationship
Student Table but i dont know how to represent Classroom_Student class in code

namespace UniversityData.Models
{
[Table("Student")]
public partial class Student
{
public Student()
{
this.Classes = new HashSet<Class>();
}
[DatabaseGenerated(DatabaseGeneratedOption.None)]
[Key]
public int StudentID { get; set; }

[Required]
[StringLength(50)]
public String StudentName { get; set; }
public ICollection<Class> Classes { get; set; }



}
}


ClassRoom Table

namespace UniversityData.Models
{
[Table("Class")]
public partial class Class
{
public Class()
{
this.Students = new HashSet<Student>();
}
[Key]
public int ClassID { get; set; }

[Required]
[StringLength(50)]
public string ClassName { get; set; }
public ICollection<Student> Students {get; set;}

}
}

Classroom_Student

StudID
ClassID
starttime
endtime
day
What i do as following
Create class for Student Table and class for ClassRoom Table
but Classroom_Student Table cannot do class for him
how to do class for Classroom_Student Table .




Responses

Posted by: Rajnilari2015 on: 11/16/2016 [Member] [Microsoft_MVP] [MVP] Platinum | Points: 50

Up
0
Down

Resolved
The following is the context class that includes Student and Class entities

public class StudentClassDBContext : DBContext
{
public StudentClassDBContextt() : base("StudentClass-DataAnnotations")
{
}

public DbSet<Student> Students { get; set; }

public DbSet<Class> Classes { get; set; }

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
}
}


--
Thanks & Regards,
RNA Team

Ahmedsa, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response