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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T04:22:26+00:00 2026-06-09T04:22:26+00:00

I am using entity framework code first and I want to mark a collection

  • 0

I am using entity framework code first and I want to mark a collection for use no lazy loading. I dont know this concept are called as eager loading. But so far I know I just have to set virtual attribute for using lazy loading. But if I dont want lazy loading, I should just leave it no virtual.

public class User
{
    public int Id { get; set; }
    public string Username { get; set; }

    public ICollection<Role> Roles { get; set; } // no virtual
}

public class Role
{
    public int Id { get; set; }
    public string Name { get; set; }

    public virtual ICollection<User> Users { get; set; } // virtual
}

In my conceptual model I always will need the Roles for the User, then I dont want lazy loading. And I dont want use Include() in my query. I just want that attribute always available.

    using (DataContext ctx = new DataContext(connectionString))
    {
        var role = ctx.Roles.Find(1);
        var users = role.Users; //lazy loading working            

        var user = ctx.Users.Find(1);

        var roles = user.Roles; //null exception
    }

But, I when I load the an User, Entity Framework give me the Roles attribute as null. Also, When I load a Role, the Users attribute works well, with lazy loading.

If I use Include, I can get it working, but I dont want use it. Also because I can’t use the Find method.

var user = ctx.Users.Include(r => r.Roles).Find(1); //the find method is not accessible
var user = ctx.Users.Include(r => r.Roles).First(u => Id == 1); //I must use this way

So, I am thinking the wrong way? Entity Framework assumes by default we alaways have to use Include to dont use lazy loading? Or I am missing some thing to get it working?

  • 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-09T04:22:28+00:00Added an answer on June 9, 2026 at 4:22 am

    You could load the collection afterwards as well:

    // Find the User entity with the given primary key value == 1
    var user = ctx.Users.Find(1); 
    
    // Loads the related Roles entities associated with the User entity
    ctx.Entry(user).Collection(u => u.Roles).Load();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello everyone my problem is very simple. Using entity framework code first I want
(USING ENTITY FRAMEWORK 4.3 CODE FIRST) The code is below. I want to actually
I'm using Entity Framework 4.3 Code First with a custom database initializer like this:
I am using Entity Framework Code First. I want to query entites from database
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
I need to map a many-to-many relationship using Entity Framework Code First. Its a
I am Using C# 2010 Entity framework code first If I have A class
I am creating a new database model using Entity Framework 4.3 Code-FIrst; using Fluent

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.