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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T01:20:28+00:00 2026-05-16T01:20:28+00:00

I am currently working on my own version of membership using Entity Framework 4.0

  • 0

I am currently working on my own version of membership using Entity Framework 4.0 and POCO.

After readying Scotts Gu blog post I decided to use conventions as much as possible.

So far I have a class called User:

public class User
{
    [System.ComponentModel.DataAnnotations.Key]
    public int UserId { get; set; }

    [System.ComponentModel.DataAnnotations.StringLength(60)] 
    public string UserName { get; set; }

    public string Password { get; set; }

    [System.ComponentModel.DataAnnotations.ConcurrencyCheck]
    public string Email { get; set; }

    [System.ComponentModel.DataAnnotations.Timestamp]
    public byte[] Timestamp { get; set; }

    public ICollection<Role> Roles { get; set; }
}

and a class called Role

public class Role
{
    [System.ComponentModel.DataAnnotations.Key]
    public int RoleId { get; set; }

    [System.ComponentModel.DataAnnotations.StringLength(50)]
    public string RoleName { get; set; }

    [System.ComponentModel.DataAnnotations.StringLength(300)]
    public string RoleDescription { get; set; }

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

I also implement DbContext like this :

public class BackboneDbContext : DbContext
{
    public DbSet<User> Users { get; set; }
    public DbSet<Role> Roles { get; set; }
}

Then based on what Scott suggested I created a Initializer class inheriting from RecreateDatabaseIfModelChanges.

Then in that class I add some dummy items like this:

    protected override void Seed(BackboneDbContext context)
    {
        var roles = new List<Role>
        {
            new Role
                {
                    RoleId = 0,
                    RoleName = "User",
                    RoleDescription = "This role belong to normal users.",
                }

        };

        roles.ForEach(r => context.Roles.Add(r));

        var users = new List<User>
        {
            new User  {UserId = 1, 
                       UserName = "Elham", 
                       Email = "abc@yahoo.com", 
                       Password = "xyz",
                       Roles = new List<Role>
                                   {
                                      roles[0]
                                   }

                       }

        };

        users.ForEach(u => context.Users.Add(u));
    }

I hope it make sense up to here. If not, basically the above code populate the tables with dummy data if database schema changes.

so far everything is absolutely great. I get the following tables:

Roles, Roles_Users and Users

they all have the info I need, but the problem is when I use the following LINQ query in my Repository class to get all the USers:

    public IQueryable<User> GetAllUsers()
    {
        IQueryable<User> allUsers = from u in _backboneDbContext.Users select u;

        return allUsers;
    }

now if I check allUsers before passing it to View, I get my users but the Role is set to ‘Null’

I don’t know why… Any Ideas ? Thank you.

  • 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-16T01:20:28+00:00Added an answer on May 16, 2026 at 1:20 am

    Try making your ICollection properties virtual. This allows EF to do lazy loading of the relationship. Alteratively, look into using the Include method on the query to eagerly load related entities.

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

Sidebar

Related Questions

I currently working on a application that use Entity Framework 4.1 (code first) and
I am currently working on my own implementation of a tab bar for a
Possible Duplicate: Programmatically delete my own app Currently I am working on a iphone
I'm currently working on several projects on my own (at least the developing part
I'm working on my own open-source project. I've recently released the first workable version
I'm currently working on implementing my own Trie in Scala (for learning/hobby purposes), and
I am using extended version of the UserCreationForm to add users via my own
I am currently working on my own little project, but I have a little
Currently working in the deployment of an OFBiz based ERP, we've come to the
Currently working on a VBScript to automate some of the dirty PST ingestion work

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.