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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T00:47:15+00:00 2026-05-25T00:47:15+00:00

I’m using Entity Framework 4.1 and trying to create a one-to-many relationship. In my

  • 0

I’m using Entity Framework 4.1 and trying to create a one-to-many relationship. In my software I have UserEntity and RoleEntity classes. Each User has one Role but one Role can have many Users.

So far I’ve succeeded in getting the data from the DB to the UserEntity except for the related RoleEntity object. The RoleId property of my test user object has the correct RoleID but Role property is null.

Is there some kind of additional configuration that I would need to write before the Role property is populated with the correct RoleEntity?

[Table("Roles")]
public class RoleEntity
{
    public long Id { get; set; }
    public string Name { get; set; }
}

[Table("Users")]
public class UserEntity
{
    public long Id { get; set; }
    public string Password { get; set; }
    public Int32 IsActive { get; set; }

    public string Username { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Email { get; set; }
    public string LocalService { get; set; }

    [ForeignKey("RoleEntity")]
    public long RoleId { get; set; }

    public virtual RoleEntity Role { get; set; }
    public virtual ClientOrganizationEntity ClientOrganization { get; set; }
}

public class UserContext : DbContext
{
    #region Constructor

    public UserContext(DbConfigurationProvider dbConfigurationProvider)
        : base(dbConfigurationProvider.ConnectionString)
    {
        if (dbConfigurationProvider == null)
            throw new ArgumentNullException("dbConfigurationProvider");

        Configuration.ProxyCreationEnabled = false;
    }

    #endregion

    #region DbSets

    public DbSet<UserEntity> Users { get; set; }
    public DbSet<RoleEntity> Roles { get; set; }
    public DbSet<ClientOrganizationEntity> ClientOrganizations { get; set; }

    #endregion
}

Data in Users table:

Id User Password FirstName LastName Email LocalService IsActive RoleId ClientOrganizationId

1 foo 62cdb7020ff920e5aa642c3d4066950dd1f01f4d FirstName LastName te@s.t foobar.com 1 1 1

Data in Roles table:

Id Name

1 Administrator

EDIT:

This how I get the data from the DbContext:

var user = _dbContext.Users.FirstOrDefault(x => x.Id == 1);
var role = user.Role;
  • 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-25T00:47:15+00:00Added an answer on May 25, 2026 at 12:47 am

    I didn’t see that you have disabled proxy creation in the constructor by

    Configuration.ProxyCreationEnabled = false
    

    Lazy loading will not work when you disable Proxy creation.

    So either Enable proxy creation or use Include method to eager load the navigational properties

    var user = _dbContext.Users.Include(u => u.Role).FirstOrDefault(x => x.Id == 1);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am reading a book about Javascript and jQuery and using one of the
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have thousands of HTML files to process using Groovy/Java and I need to
I'm trying to create an if statement in PHP that prevents a single post
I'm making a simple page using Google Maps API 3. My first. One marker
I am trying to loop through a bunch of documents I have to put
I want to count how many characters a certain string has in PHP, but
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has

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.