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 7576413
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T16:54:51+00:00 2026-05-30T16:54:51+00:00

To introduce myself to the entity framework I created a console application that works

  • 0

To introduce myself to the entity framework I created a console application that works with it.

There are two entities: ClassA and ClassB. There is a one-to-many relationship between them. One instance of ClassA can have multiple instances of ClassB. One instance of ClassB has 0 or 1 instance of ClassA.

The code for the classes is:

public class ClassA {
    public virtual int Id {get; set;}
    public virtual string Name {get; set;}
    public virtual ICollection<ClassB> ClassBs {get; set;}
}

public class ClassB {
    public virtual int Id {get; set;}
    public virtual string Name {get; set;}
    public virtual ClassA ClassA {get; set;}
}

The code for my database context and initializer is:

public class Context : DbContext {
    public DbSet<ClassA> ClassAs {get; set;}
    public DbSet<ClassB> ClassBs {get; set;}

    protected override void OnModelCreating(DbModelBuilder modelBuilder) {
        modelBuilder.Entity<ClassA>()
            .HasMany(classA => classA.ClassBs)
            .WithRequired(classB => classB.ClassA);
        base.OnModelCreating(modelBuilder);
    }
}

public class Initializer : DropCreateDatabaseAlways<Context> {
    protected override void Seed(Context context) {
        base.Seed(context);
        for (int i = 1; i <= 3; i++) {
            var classA = new ClassA{
                Name = "A-" + i,
                ClassBs = new LinkedList<ClassB>()
            };
            for (int j = 1; j <= 3; j++) {
                var classB = new ClassB{
                    Name = "B-" + i + "-" + j,
                    ClassA = classA
                };
                classA.ClassBs.Add(classB);
                context.ClassBs.Add(classB);
            }
            context.ClassAs.Add(classA);
        }
        context.SaveChanges();
    }
}

My Main method is:

    static void Main(string[] args) {
        Database.SetInitializer(new Initializer());

        Context db = new Context();

        foreach (var classA in db.ClassAs) {
            Console.WriteLine(classA.Name);
            foreach (var classB in classA.ClassBs)
                Console.WriteLine("\t" + classB.Name);
        }

        Console.Write("\nFIN");
        Console.ReadKey();
    }

When the second foreach loop in the Main method starts the following exception is thrown:

System.Data.EntityCommandExecutionException was unhandled
  Message=An error occurred while executing the command definition. See the inner exception for details.
  Source=System.Data.Entity
  InnerException: System.InvalidOperationException
       Message=There is already an open DataReader associated with this Command which must be closed first.
       Source=System.Data
       StackTrace:
            at System.Data.SqlClient.SqlInternalConnectionTds.ValidateConnectionForExecute(SqlCommand command)
            at System.Data.SqlClient.SqlConnection.ValidateConnectionForExecute(String method, SqlCommand command)
            at System.Data.SqlClient.SqlCommand.ValidateCommand(String method, Boolean async)
            at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result)
            at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)
            at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method)
            at System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior)
            at System.Data.Common.DbCommand.ExecuteReader(CommandBehavior behavior)
            at System.Data.EntityClient.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand entityCommand, CommandBehavior behavior)

It seems that the ClassBs property in ClassA is not loading properly. However I did specify in the context that ClassA has many ClassB.

The database connection seems fine, since the first instance of ClassA is displayed on the console. I can also acces the database in Visual Studio and look at the data generated in the initializer.

I used the training videos of ASP.NET MVC to get started with this console application.

Do you know what’s going wrong?

  • 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-05-30T16:54:52+00:00Added an answer on May 30, 2026 at 4:54 pm

    You need to set multipleactiveresultsets in your connection string to true.

    MultipleActiveResultSets=true;

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i've started to introduce myself into REST. I use as REST-framework Restlet. I have
I'm looking to introduce a unit testing framework into the mix at my job.
I am trying to introduce RoR to my company and I have two ways
This is an easy program to introduce the Reactive Framework . But I want
I'm developing in LAMP (Linux+Apache+MySQL+PHP) since I remember myself. But one question was bugging
I see there's a new version of the managed EWS API v1.2 that targets
I have an an MVC 2 web application that uses Master pages. In the
I'm trying to introduce a checkbox next to another element. The problem is that
I'm having trouble getting the windsor container and entity framework working together and it
This might be one of those questions that are difficult to answer, but here

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.