Issue in fetching data from multiple tables using joins in Entity Framework in windows form C#

Last Reply 2 days ago By dharmendr

Posted 24 days ago

I am working on Entity Framework. I wrote the following two classes.

Student.cs

 

 

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace myApp
{
    public class Student
    {
        //scalar
        [Key]
        public int StuId { get; set; }
        [StringLength(255)]
        public string StuName { get; set; }
         [StringLength(255)]
        public string StuFName { get; set; }
         [StringLength(255)]
        public string StuPhone { get; set; }
         [StringLength(255)]
        public string StuAddress { get; set; }
        public Nullable<int> StuType { get; set; }
        public DateTime? DateOfBirth { get; set; }
        public bool Status { get; set; }
        public string Image { get; set; }
        [NotMapped]
        public int ObjectState { get; set; }

        //reference navigation
        public int GradeId { get; set; }
        public Grade Grade { get; set; }

    }

    public class Grade
    {
        [Key]
        public int GradeId { get; set; }
        [StringLength(255)]
        public string GradeName { get; set; }
        [NotMapped]
        public int ObjectState { get; set; }
        public ICollection<Student> Student { get; set; }
    }

    
}

another class SchoolContext.cs

 

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace myApp
{
    public class SchoolContext : DbContext
    {
        public SchoolContext() : base("name=cn") { }

        //classes become entities when included as 
        public DbSet<Student> Students { get; set; }
        public DbSet<Grade> Grades { get; set; }
        
    }


    
}

After that I designed two form

1- frmStuGrade which contains

GradeID                  GradeName

2- frmStudents which contains

StuID   StuName  StuAddress,  Grade 

screenshoot is attached hereby

dsfsdfsadfgsdag

I wrote the following code to insert the data in the database.

 

private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                using (SchoolContext db = new SchoolContext())
                {
                    Student obj = studentBindingSource.Current as Student;
                    if (obj != null)
                    {
                        if (db.Entry<Student>(obj).State == System.Data.Entity.EntityState.Detached)
                            db.Set<Student
                                >().Attach(obj);
                        if (obj.ObjectState == 1)
                            db.Entry<Student>(obj).State = System.Data.Entity.EntityState.Added;
                        else if (obj.ObjectState == 2)
                            db.Entry<Student>(obj).State = System.Data.Entity.EntityState.Modified;
                        db.SaveChanges();
                        dGV.Refresh();
                        pContainer.Enabled = false;
                        obj.ObjectState = 0;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

Now I want to show data in datagridview from multiple tables using joins. I am unable to fetch data. how to get solution?

You are viewing reply posted by: dharmendr 16 days ago.