none
Code First Help me please RRS feed

  • Question

  • I Create dataBase using Code First base on this entity

    Code First Automatic Creatin DataBase

    How I Can Get Data From Table StudentKurses????


    var queryKurses = from q in db.Kursess select q; This Query not return Collections University and Students

    This query not work var qu = db.Database.SqlQuery<Class1>("SELECT * FROM dbo.StudentKurses");

    Thursday, July 14, 2016 7:03 PM

Answers

  • Hi Ahlompys,

    >>How I Can Get Data From Table StudentKurses????

    According to your description, I create a demo by using your code, We could use Lazy Loading, Eagerly Loading and raw SQL to retrieve database from Table StudentKurses.

    #Lazy Loading (as you provide)

                    var query = from q in db.Kursess select q;
    
                    foreach (var item in query)
                    {
                        Console.WriteLine("item: {0}", item.Subject);
                        foreach (var subitem in item.Students)
                        {
                            Console.WriteLine("SubStu: {0}", subitem.Address);
                        }
                        foreach (var subitem in item.Universitys)
                        {
                            Console.WriteLine("SubUni: {0}", subitem.Title);
                        }
                    }

    #Eagerly Loading

                    var query = from q in db.Kursess.Include("Universitys").Include("Students") select q;
    
                    foreach (var item in query)
                    {
                        Console.WriteLine("item: {0}", item.Subject);
                        foreach (var subitem in item.Students)
                        {
                            Console.WriteLine("SubStu: {0}", subitem.Address);
                        }
    
                        foreach (var subitem in item.Universitys)
                        {
                            Console.WriteLine("SubUni: {0}", subitem.Title);
                        }
                    }

    For more information about Loading Related Entities, please refer to:

    https://msdn.microsoft.com/en-sg/data/jj574232.aspx

    >>This query not work var qu = db.Database.SqlQuery<Class1>("SELECT * FROM dbo.StudentKurses");

    From my demo, it works well, and my development is: entity framework 6.1.3, .net framework 4.6.1, vs2015

    #Raw SQL.

    var result = db.Database.SqlQuery<Class1>("SELECT * FROM dbo.StudentKurses");
    
                    foreach (var item in result)
                    {
                        Console.WriteLine("{0} -- {1}", item.KursesID, item.StudentID);
                    }

    #Models Class.

    using System.Collections.Generic;
    
    namespace ConsoleApplication2
    {
        
        public class Class1
        {
            
    
            public int KursesID { get; set; }
    
            public int StudentID { get; set; }
        }
    
    
    
        public class Kurses
        {
            public Kurses()
            {
                this.Universitys = new HashSet<University>();
                this.Students = new HashSet<Student>();
            }
    
            public int KursesID { get; set; }
            public string Subject { get; set; }
            public int ReadHour { get; set; }
    
            public virtual ICollection<University> Universitys { get; set; }
            public virtual ICollection<Student> Students { get; set; }
        }
    
        public class University
        {
            public University()
            {
                this.Kursess = new HashSet<Kurses>();
                this.Students = new HashSet<Student>();
            }
    
            public int UniversityID { get; set; }
            public string Title { get; set; }
    
            public string Address { get; set; }
    
            public virtual ICollection<Kurses> Kursess { get; set; }
    
            public virtual ICollection<Student> Students { get; set; }
        }
    
        public class Student
        {
            public Student()
            {
                this.Kursess = new HashSet<Kurses>();
                this.Universitys = new HashSet<University>();
            }
    
            public int StudentID { get; set; }
            public string Name { get; set; }
            public string SurName { get; set; }
            public string Address { get; set; }
            public virtual ICollection<Kurses> Kursess { get; set; }
    
            public virtual ICollection<University> Universitys { get; set; }
        }
    }
    

    #DbContext

    namespace ConsoleApplication2
    {
        using System.Data.Entity;
    
        public partial class Model1 : DbContext
        {
            public Model1()
                : base("name=Model1")
            {
            }
    
            public DbSet<Case> Cases { get; set; }
            public DbSet<CaseType> CaseTypes { get; set; }
    
            public DbSet<Kurses> Kursess { get; set; }
            public DbSet<Student> Students { get; set; }
            public DbSet<University> Universitys { get; set; }
            protected override void OnModelCreating(DbModelBuilder modelBuilder)
            {
                modelBuilder.Entity<Kurses>()
                    .HasMany<Student>(s => s.Students)
                    .WithMany(c => c.Kursess)
                    .Map(cs =>
                    {
                        cs.MapLeftKey("KursesID");
                        cs.MapRightKey("StudentID");
                        cs.ToTable("StudentKurses");
                    });
    
                modelBuilder.Entity<Kurses>()
                    .HasMany<University>(s => s.Universitys)
                    .WithMany(c => c.Kursess)
                    .Map(cs =>
                    {
                        cs.MapLeftKey("KursesID");
                        cs.MapRightKey("UniversityID");
                        cs.ToTable("UniversityKurses");
                    });
    
                modelBuilder.Entity<University>()
                    .HasMany<Student>(s => s.Students)
                    .WithMany(c => c.Universitys)
                    .Map(cs =>
                    {
                        cs.MapLeftKey("UniversityID");
                        cs.MapRightKey("StudentID");
                        cs.ToTable("UniversityStudents");
                    });
    
              }
        }
    }
    

    #Lazy Loading, Eagerly Loading

    #RAW SQL

    Best regards,

    Cole Wu


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by Ahlompys Friday, July 15, 2016 9:47 AM
    Friday, July 15, 2016 2:38 AM
    Moderator

All replies

  • Hi Ahlompys,

    >>How I Can Get Data From Table StudentKurses????

    According to your description, I create a demo by using your code, We could use Lazy Loading, Eagerly Loading and raw SQL to retrieve database from Table StudentKurses.

    #Lazy Loading (as you provide)

                    var query = from q in db.Kursess select q;
    
                    foreach (var item in query)
                    {
                        Console.WriteLine("item: {0}", item.Subject);
                        foreach (var subitem in item.Students)
                        {
                            Console.WriteLine("SubStu: {0}", subitem.Address);
                        }
                        foreach (var subitem in item.Universitys)
                        {
                            Console.WriteLine("SubUni: {0}", subitem.Title);
                        }
                    }

    #Eagerly Loading

                    var query = from q in db.Kursess.Include("Universitys").Include("Students") select q;
    
                    foreach (var item in query)
                    {
                        Console.WriteLine("item: {0}", item.Subject);
                        foreach (var subitem in item.Students)
                        {
                            Console.WriteLine("SubStu: {0}", subitem.Address);
                        }
    
                        foreach (var subitem in item.Universitys)
                        {
                            Console.WriteLine("SubUni: {0}", subitem.Title);
                        }
                    }

    For more information about Loading Related Entities, please refer to:

    https://msdn.microsoft.com/en-sg/data/jj574232.aspx

    >>This query not work var qu = db.Database.SqlQuery<Class1>("SELECT * FROM dbo.StudentKurses");

    From my demo, it works well, and my development is: entity framework 6.1.3, .net framework 4.6.1, vs2015

    #Raw SQL.

    var result = db.Database.SqlQuery<Class1>("SELECT * FROM dbo.StudentKurses");
    
                    foreach (var item in result)
                    {
                        Console.WriteLine("{0} -- {1}", item.KursesID, item.StudentID);
                    }

    #Models Class.

    using System.Collections.Generic;
    
    namespace ConsoleApplication2
    {
        
        public class Class1
        {
            
    
            public int KursesID { get; set; }
    
            public int StudentID { get; set; }
        }
    
    
    
        public class Kurses
        {
            public Kurses()
            {
                this.Universitys = new HashSet<University>();
                this.Students = new HashSet<Student>();
            }
    
            public int KursesID { get; set; }
            public string Subject { get; set; }
            public int ReadHour { get; set; }
    
            public virtual ICollection<University> Universitys { get; set; }
            public virtual ICollection<Student> Students { get; set; }
        }
    
        public class University
        {
            public University()
            {
                this.Kursess = new HashSet<Kurses>();
                this.Students = new HashSet<Student>();
            }
    
            public int UniversityID { get; set; }
            public string Title { get; set; }
    
            public string Address { get; set; }
    
            public virtual ICollection<Kurses> Kursess { get; set; }
    
            public virtual ICollection<Student> Students { get; set; }
        }
    
        public class Student
        {
            public Student()
            {
                this.Kursess = new HashSet<Kurses>();
                this.Universitys = new HashSet<University>();
            }
    
            public int StudentID { get; set; }
            public string Name { get; set; }
            public string SurName { get; set; }
            public string Address { get; set; }
            public virtual ICollection<Kurses> Kursess { get; set; }
    
            public virtual ICollection<University> Universitys { get; set; }
        }
    }
    

    #DbContext

    namespace ConsoleApplication2
    {
        using System.Data.Entity;
    
        public partial class Model1 : DbContext
        {
            public Model1()
                : base("name=Model1")
            {
            }
    
            public DbSet<Case> Cases { get; set; }
            public DbSet<CaseType> CaseTypes { get; set; }
    
            public DbSet<Kurses> Kursess { get; set; }
            public DbSet<Student> Students { get; set; }
            public DbSet<University> Universitys { get; set; }
            protected override void OnModelCreating(DbModelBuilder modelBuilder)
            {
                modelBuilder.Entity<Kurses>()
                    .HasMany<Student>(s => s.Students)
                    .WithMany(c => c.Kursess)
                    .Map(cs =>
                    {
                        cs.MapLeftKey("KursesID");
                        cs.MapRightKey("StudentID");
                        cs.ToTable("StudentKurses");
                    });
    
                modelBuilder.Entity<Kurses>()
                    .HasMany<University>(s => s.Universitys)
                    .WithMany(c => c.Kursess)
                    .Map(cs =>
                    {
                        cs.MapLeftKey("KursesID");
                        cs.MapRightKey("UniversityID");
                        cs.ToTable("UniversityKurses");
                    });
    
                modelBuilder.Entity<University>()
                    .HasMany<Student>(s => s.Students)
                    .WithMany(c => c.Universitys)
                    .Map(cs =>
                    {
                        cs.MapLeftKey("UniversityID");
                        cs.MapRightKey("StudentID");
                        cs.ToTable("UniversityStudents");
                    });
    
              }
        }
    }
    

    #Lazy Loading, Eagerly Loading

    #RAW SQL

    Best regards,

    Cole Wu


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by Ahlompys Friday, July 15, 2016 9:47 AM
    Friday, July 15, 2016 2:38 AM
    Moderator
  • Cole Wu   !!!Very Big THANKS!!!! 
    Friday, July 15, 2016 9:49 AM