In Index() method of School controller, Which code need to write by me? Please, explain. I don't want to use fluent API. I want to implement it using Data Annotation.
In Model Folder, I have created Student.cs class
- public class Student
- {
- public int StudentId { get; set; }
- public string StudentName { get; set; }
- public ICollection<Course> CoursesNProperty { get; set; }
- }
In Model Folder, I have created Course.cs class- public class Course
- {
- public int CourseId { get; set; }
- public string CourseName { get; set; }
- public ICollection<Student> StudentsNProperty { get; set; }
- }
In Model Folder, I have created SchoolContext.cs class- public class SchoolContext :DbContext
- {
- public SchoolContext() : base("name=conStr") { }
- public DbSet<Student> Students { get; set; }
- public DbSet<Course> Courses { get; set; }
- }
In web.config file- <connectionStrings>
- <add name="conStr" connectionString="Data Source=Dinesh\Aaradhya;Database=GopinathSchoolMToM;User Id=sa;Password=test123" providerName="System.Data.SqlClient"/>
- </connectionStrings>
In controller Folder, I have created School controller- using Note7Page124CascadeMToM.Models;
- namespace Note7Page124CascadeMToM.Controllers
- {
- public class SchoolController : Controller
- {
- SchoolContext db = new SchoolContext();
-
- public ActionResult Index()
- {
- Student student1 = new Student()
- {
- StudentName = "Prakash"
- };
- Student student2 = new Student()
- {
- StudentName = "Shubham"
- };
- Course course1 = new Course()
- {
- CourseName = "Asp.Net"
- };
- Course course2 = new Course()
- {
- CourseName="C#.Net"
- };
-
- return View();
- }
- }
- }