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
CoursesNProperty { get; set; } - }
- public class Course
- {
- public int CourseId { get; set; }
- public string CourseName { get; set; }
- public ICollection
StudentsNProperty { get; set; } - }
- public class SchoolContext :DbContext
- {
- public SchoolContext() : base("name=conStr") { }
- public DbSet
Students { get; set; } - public DbSet
Courses { get; set; } - }
- <connectionStrings>
- <add name="conStr" connectionString="Data Source=Dinesh\Aaradhya;Database=GopinathSchoolMToM;User Id=sa;Password=test123" providerName="System.Data.SqlClient"/>
- connectionStrings>
- using Note7Page124CascadeMToM.Models;
- namespace Note7Page124CascadeMToM.Controllers
- {
- public class SchoolController : Controller
- {
- SchoolContext db = new SchoolContext();
- // GET: School
- 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"
- };
- //Which code need to implement here? ...
- return View();
- }
- }
- }

Jenith ThakkarPosted Jul 15, 2018, 4:29 AM