jeudi 14 mai 2015

How to use WebApi along with MVC 5 in Student - course model - ASP.NET

I have experience of developing ASP.NET-MVC application and learning WebApi very recently. I understood that action method in API are HTTP, hence can directly the resulting model. I am also aware that I can use MVC along with WebApi, my struggling to understand where these two fit together and where should I use WebAPI and where not in single MVC-based application; taking example from following; my 2nd question is where I apply MVC and WebApi

enter image description here

Student Model

 public partial class Student
{
    public Student()
    {
        this.StudentCourses = new HashSet<StudentCourse>();
    }

    public int StudentID { get; set; }
    public string Name { get; set; }

    public virtual ICollection<StudentCourse> StudentCourses { get; set; }
}

Course Model

 public partial class Course
{
    public Course()
    {
        this.StudentCourses = new HashSet<StudentCourse>();
    }

    public int CourseID { get; set; }
    public string Title { get; set; }

    public virtual ICollection<StudentCourse> StudentCourses { get; set; }
}

StudentCourseModel

public partial class StudentCourse
{
    [Key]
    public int StudentCourseID { get; set; }

    [Key]
    [ForeignKey("Student")]
    public int StudentID { get; set; }

    [Key]
    [ForeignKey("Course")]
    public int CourseID { get; set; }

    public virtual Course Course { get; set; }
    public virtual Student Student { get; set; }
}

Function Logic of App

 public class processData
{
    public void addStudent()
    {
        using(var db = new MyDbContext())
        {
            var _student1 = new Student { Name = "waqas" };

            db.Students.Add(_student1);

            db.SaveChanges();
        }

        Console.WriteLine("Press Any Key to Read Students....");
        Console.ReadLine();

        readStudents();
    }


    public void readStudents()
    {
        using (var db2 = new MyDbContext())
        {
            List<Student> _studentRead = new List<Student>();

            _studentRead = (from _student in db2.Students
                                .Include(r => r.StudentCourses.Select(sc => sc.Course))
                                select _student).ToList();

        }
    }

    public void CreateCourse()
    {
        using(var db3 = new MyDbContext())
        {
            var _course1 = new Course { Title = "Math"};
            var _course2 = new Course { Title = "English" };
            var _course3 = new Course { Title = "computing" };
            var _course4 = new Course { Title = "Science" };

            db3.Courses.Add(_course1);
            db3.Courses.Add(_course2);
            db3.Courses.Add(_course3);
            db3.Courses.Add(_course4);

            db3.SaveChanges();
        }
    }

    public void ReadCourses()
    {
        using(var db4 = new MyDbContext())
        {
            var _query2 = from c in db4.Courses
                          orderby c.Title
                          select c;

            foreach(var item in _query2)
            {
                Console.WriteLine(item.CourseID + "     " + item.Title);
            }
        }
    }

    public void AddCourseToStudent()
    {
        using(var db5 = new MyDbContext())
        {
            var _registeration1 = new StudentCourse { StudentID = 7, CourseID = 6};
            db5.StudentCourses.Add(_registeration1);

            var _registeration2 = new StudentCourse { StudentID = 7, CourseID = 8 };
            db5.StudentCourses.Add(_registeration2);

            var _registeration3 = new StudentCourse { StudentID = 7, CourseID = 9 };
            db5.StudentCourses.Add(_registeration3);

            db5.SaveChanges();

        }
    }

Aucun commentaire:

Enregistrer un commentaire