Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8679395
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T20:57:42+00:00 2026-06-12T20:57:42+00:00

Im using entity framework code first 4.3 in c# I got the following classes:

  • 0

Im using entity framework code first 4.3 in c#

I got the following classes:

Class A
{
public string Name { get; set; }
public List<B> Bs { get; set; }
}

Class B
{
public string Name { get; set; }
}

There is one to many realationship from A to B.
When i try to load B list from A. I only get the first element from the list.

When i execute the following code, i expect a2 to contain both b’s, but actually it contains only one. does anyone can help spot the problem?

B b = new B() {Name = "b"};
A a = new A() {Name = "a", 
Bs = new List<B>() { new B() {Name = "b1"}, new B() {Name = "b2"} };

using (var context = new MyContext())
{
context.As.Add(a);
context.SaveChanges();
}
using (var context = new MyContext())
{
var a2 = (from a in context.As.Include(a => a.Bs)
        where a.Name == "a"
        select a).Single();
}

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T20:57:43+00:00Added an answer on June 12, 2026 at 8:57 pm

    Can you post your actual code – the code you posted doesn’t compile, but with some small changes to some of your brackets and variable naming it does – but I get a different result than you, a2 contains both B’s, as expected, so the problem may lie somewhere else. Here’s the entire console app:

    public class A
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public List<B> Bs { get; set; }
    }
    
    public class B
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }
    
    public class Model : DbContext
    {
        public DbSet<A> As { get; set; }
        public DbSet<B> Bs { get; set; }
    }
    
    class Program
    {
        static void Main(string[] args)
        {
            var b = new B() { Name = "b" };
            var a1 = new A()
                {
                    Name = "a",
                    Bs = new List<B>() { new B() { Name = "b1" }, new B() { Name = "b2" } }
                };
    
            using (var context = new Model())
            {
                context.As.Add(a1);
                context.SaveChanges();
            }
            using (var context = new Model())
            {
                var a2 = (from a in context.As.Include(a => a.Bs)
                          where a.Name == "a"
                          select a).Single();
    
                Console.WriteLine(a2.Bs.Count);
            }
    
            Console.ReadLine();
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using Entity Framework Code First, I have something like: public class Foo { public
I am Using C# 2010 Entity framework code first If I have A class
I've tried using Entity Framework Code First and set AutomaticMigrationsEnabled to true so that
I'm using Code First notation of Entity Framework, and got some problem after changing
We are using Entity Framework 4.1 Code first. We've got user entity with primary
I am using Entity Framework Code First and have some Classes with a System.Net.Mail.MailAddress
I am using Entity Framework Code First Approach. I have following code to insert
When using Entity Framework Code First 4.3.1 it is possible to create relationships with
We are using Entity Framework Code First with Foreign Key relationships. We investigating on
I'm using Entity Framework Code First v 4.3.0 I have a two entities with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.