none
Join in MVC Application RRS feed

  • Question

  • Hello,

    I have a DB with multiple tables. I have set the PK and the FK but I seem to only see the parents from the children but the other way around. I want to be able to display them in a single view. Thank you for answering in advance.

    Friday, December 18, 2015 12:37 PM

Answers

  • Hi persephona,

    You could try to use entity framework.

    Below is a demo for you reference.

    public class Blog
    {
         public int Id { get; set; }
         public string Name { get; set; }
         public string Url { get; set; }     
         public virtual List<Post> Posts { get; set; }
    }
    public class Post
    {
        public int Id { get; set; }
        public string Title { get; set; }
        public string Content { get; set; }
        public virtual Blog Blog { get; set; }
    }
    
    public class BlogContext : DbContext
    {
        public BlogContext()
                : base("name=Blogging")
        {
          Database.SetInitializer<BlogContext>(new CreateDatabaseIfNotExists<BlogContext>());
        }
        public DbSet<Blog> Blogs { get; set; }
        public DbSet<Post> Posts { get; set; }
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
           base.OnModelCreating(modelBuilder);
           Database.SetInitializer<BlogContext>(new CreateDatabaseIfNotExists<BlogContext>());
    }
    
    static void Main(string[] args)
    {
       using (var db = new BlogContext())
       {           
           var blog = db.Blogs.Include(p=> p.Posts).Where(b => b.Id.Equals(1)).FirstOrDefault();
           Console.WriteLine(blog.Name + "aaa");
           foreach(var post in blog.Posts)
           {
               Console.WriteLine(post.Content);
           }
           Console.ReadKey();
         }
    }

    Best regards,


    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.

    Monday, December 21, 2015 3:01 AM
    Moderator

All replies

  • Hi persephona,

    You could try to use entity framework.

    Below is a demo for you reference.

    public class Blog
    {
         public int Id { get; set; }
         public string Name { get; set; }
         public string Url { get; set; }     
         public virtual List<Post> Posts { get; set; }
    }
    public class Post
    {
        public int Id { get; set; }
        public string Title { get; set; }
        public string Content { get; set; }
        public virtual Blog Blog { get; set; }
    }
    
    public class BlogContext : DbContext
    {
        public BlogContext()
                : base("name=Blogging")
        {
          Database.SetInitializer<BlogContext>(new CreateDatabaseIfNotExists<BlogContext>());
        }
        public DbSet<Blog> Blogs { get; set; }
        public DbSet<Post> Posts { get; set; }
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
           base.OnModelCreating(modelBuilder);
           Database.SetInitializer<BlogContext>(new CreateDatabaseIfNotExists<BlogContext>());
    }
    
    static void Main(string[] args)
    {
       using (var db = new BlogContext())
       {           
           var blog = db.Blogs.Include(p=> p.Posts).Where(b => b.Id.Equals(1)).FirstOrDefault();
           Console.WriteLine(blog.Name + "aaa");
           foreach(var post in blog.Posts)
           {
               Console.WriteLine(post.Content);
           }
           Console.ReadKey();
         }
    }

    Best regards,


    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.

    Monday, December 21, 2015 3:01 AM
    Moderator
  • We need more information as to how you're trying to accomplish this. What methodology are you currently using (or are you asking for advice as to what to use)? Do you have any relevant code you can share?

    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    Monday, December 21, 2015 6:02 AM
  • Thank you it worked.
    Monday, December 21, 2015 12:17 PM