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

  • Home
  • SEARCH
  • 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 9196563
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T21:52:38+00:00 2026-06-17T21:52:38+00:00

UPDATED : I understood I should not use a DbSet so I changed the

  • 0

UPDATED: I understood I should not use a DbSet so I changed the implementation to an ICollection as suggested by Erenga

Please consider the following classes:

[Table("Tenant")]
public class Tenant : IEntity
{
    public int Id { get; set; }
    public string Name { get; set; }
    [Key]
    public string Guid { get; set; }
    public virtual ICollection<User> Users { get; set; }
}

[Table("User")]
public class User : IEntity
{
    public int Id { get; set; }

    public string Name { get; set; }
    public string EmailAddress { get; set; }
    public string Password { get; set; }
}

The first test creates a new Tenant and a new User and stores them in the appropriate tables.

    [Test]
    public void CreateNewUserForNewTenant()
    {
        var user = _applicationContext.Users.Create();
        user.Name = "barney";
        user.EmailAddress = "barney@flinstone.com";

        var tenant = _applicationContext.Tenants.Create();
        tenant.Name = "localhost";
        tenant.Guid = Guid.NewGuid().ToString();

        tenant.Users.Add(user); // NullReferenceException, I expected the EF would LazyLoad the reference to Users?!
        _tenantRepository.Add(tenant);
        _applicationContext.SaveChanges();
    }

This test will fail on a NullReferenceException since the property Users is not initialized.

How should I change my code that I can rely on LazyLoading provided with EF?

  • 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-17T21:52:40+00:00Added an answer on June 17, 2026 at 9:52 pm

    There are 2 problems I see here.

    1. As @SimonWhitehead mentioned, reference types are initialized as null by default. Lazy loading works only on entities created by EF. These are actually sub classes of your class that contain addtional logic to lazy load.

    2. DbSet is not a collection type that is supported on entities. You need to change the type to ICollection, ISet, or IList.

    Here’s a working example

    [Table("Tenant")]
    public class Tenant : IEntity
    {
        public int Id { get; set; }
        public string Name { get; set; }
        [Key]
        public string Guid { get; set; }
        public virtual ICollection<User> Users { get; set; }
    }
    
    [Table("User")]
    public class User : IEntity
    {
        public int Id { get; set; }
    
        public string Name { get; set; }
        public string EmailAddress { get; set; }
        public string Password { get; set; }
    }
    
    [Test]
    public void CreateNewUserForNewTenant()
    {
        var user = _applicationContext.Users.Create();
        user.Name = "barney"; 
        user.EmailAddress = "barney@flinstone.com";
    
        var tenant = _applicationContext.Tenents.Create();
        tenant.Name = "localhost";
        tenant.Guid = Guid.NewGuid().ToString();
    
        tenant.Users = new List<User> { user };
        _tenantRepository.Add(tenant);
        _applicationContext.SaveChanges();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using the following steps: (I have checked this similar post , which does not
I saw this sentence not only in one place: A transaction should be kept
I updated some of my CSS styles to use normalize.css recently but discovered an
I am currently getting confused why the following works in some cases but not
NOTE: Updated and rewritten This question has been redone and updated. Please pardon outdated
I use Yii, and I get this error; What should I understand and do?
I dont understand why I am not able to update a field on the
Please help me with this update query. I COMPLETELY understand how redundant this is
Updated-2 I have interesting combination of warnings & errors. Firstly, when debugging, i get
Updated Example: http://jsfiddle.net/7Cwbn/62/ You can click on the markers Hold Ctrl to select multiple

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.