TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Dinesh Kudale
NA
127
17.9k
How to implement many to many relationship in Asp.Net Mvc ..
Jul 15 2018 2:09 AM
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();
// 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();
}
}
}
Reply
Answers (
1
)
we need to do populate dropdownlist from database in dynamic
bootstrap datetimepicket in .net core app example