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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:55:44+00:00 2026-06-14T21:55:44+00:00

I’m using entity framework 5. I’m trying to lazy load an entity collection, I

  • 0

I’m using entity framework 5.
I’m trying to lazy load an entity collection, I stripped the model to the bare bones to have a simple runable sample.

This is my model:

public class A
{

    public int Id { get; set; }

    public EntityCollection<B> Bs
    {
        get { return bs; }
        set { bs = value; }
    }
    private EntityCollection<B> bs;

    public A()
    {
        bs = new EntityCollection<B>();
    }
}

public class B
{
    public int Id { get; set; }
    public A A { get; set; }
}

public class DbModel : DbContext
{
    public DbSet<A> As { get; set; }
    public DbSet<B> Bs { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<A>()
                    .HasKey( t => t.Id )
                    .HasMany(a => a.Bs)
                    .WithRequired(b => b.A);
        modelBuilder.Entity<A>()
                    .Property(t => t.Id)
                    .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);

        modelBuilder.Entity<B>()
                    .HasKey(b => b.Id)
                    .HasRequired(b => b.A);
        modelBuilder.Entity<B>()
                    .Property( t => t.Id )
                    .HasDatabaseGeneratedOption( DatabaseGeneratedOption.Identity );
    }

This is the test demonstrating my problem:

   [TestInitialize]
    public void Initialize()
    {
        model = new DbModel();
        model.Configuration.ProxyCreationEnabled = false;

        if (model.Database.Exists()) model.Database.Delete();

        model.Database.Create();
        A a = model.As.Create();
        model.As.Add(a);
        B b = model.Bs.Create();
        a.Bs.Add(b);

        model.ChangeTracker.DetectChanges();
        model.SaveChanges();
    }

   [TestMethod]
    public void TestMethod1()
    {
        // arrange
        DbModel tModel = new DbModel();
        A a = tModel.As.First();

        // act
        a.Bs.Load();
    }

This is the result:

Test method TestProject1.UnitTest1.TestMethod1 threw exception:

System.InvalidOperationException: Requested operation is not allowed
when the owner of this RelatedEnd is null. RelatedEnd objects that
were created with the default constructor should only be used as a
container during serialization.

  • 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-14T21:55:45+00:00Added an answer on June 14, 2026 at 9:55 pm

    As it turns out I was using classes from different paradigms of the ADO family.
    As pointed out by Gert Arnold EF doesn’t work with EntityCollection it uses a plain list for association collections.

    public class A
    {
    
        public int Id { get; set; }
    
        public List<B> Bs
        {
            get { return bs; }
            set { bs = value; }
        }
        private List<B> bs = new List<B>();
    }
    

    As I did not want the proxies, the objects have no “knowledge” of the model. So the model itself has to be used to load associations for an entity.
    For this sample I added the following method to the model:

    public class DbModel : DbContext
    {
        public DbSet<A> As { get; set; }
        public DbSet<B> Bs { get; set; }
    
    //…
    
        public A LoadAWithAssociations(A targetA)
        {
            return As.Include("Bs").First(a => a.Id.Equals(targetA.Id));
        }
    }
    

    I adapted the test to use the new method

      [TestMethod]
        public void TestMethod1()
        {
            // arrange
            DbModel tModel = new DbModel();
            A a = tModel.As.First();
    
            // act
            A result =  tModel.LoadAWithAssociations(a);
    
            // assert 
            Assert.IsNotNull(result);
            Assert.IsTrue(result.Bs.Count > 0);
        }
    

    It gives a green now

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

Sidebar

Related Questions

Trying to map the following schema using the Entity Framework. A Customer can have
I'm trying to make sense of a situation I have using entity framework on
I'm trying to insert some data in my database using Entity Framework model, but
I'm using Entity Framework 4.1 Code First and have been trying to add a
I've been trying to get started using entity framework and have ran across a
I have a project where the client is using Entity Framework, and I'm trying
Using Entity Framework 4, I'm trying to implement dynamic sorting based on a collection
I am trying to load data in to datagrid of SilverLight using Entity Framework
I am using Entity Framework. I am trying to get the results from linq
Working on a project using Entity Framework (4.3.1.0). I'm trying to figure out how

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.