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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T10:02:01+00:00 2026-05-31T10:02:01+00:00

Here is my source code for my model: public class User { public User()

  • 0

Here is my source code for my model:

public class User
{
    public User()
    {
        GUID = Guid.NewGuid();
        Account = new Account();
        Location = new Location();
    }

    public long UserID { get; set; }
    public Guid GUID { get; set; }
    public string DisplayName { get; set; }
    public Location Location { get; set; }
    public virtual Account Account { get; set; }
}

public class UserConfiguration : EntityTypeConfiguration<User>
{
    public UserConfiguration()
    {
        HasKey(x => x.UserID);
    }
}

[ComplexType]
public class Location
{
    [MaxLength(2)]
    public string CountryCode { get; set; }
    [MaxLength(2)]
    public string StateCode { get; set; }
    public string City { get; set; }
}

public class Account
{
    public Account()
    {
        if (EmailAddresses == null) EmailAddresses = new Collection<EmailAddress>();
    }

    [ForeignKey("User")]
    public long AccountID { get; set; }

    public ICollection<EmailAddress> EmailAddresses { get; set; }

    public virtual User User { get; set; }
}

public class AccountConfiguration : EntityTypeConfiguration<Account>
{
    public AccountConfiguration()
    {
        HasKey(x => x.AccountID);
        HasMany(x => x.EmailAddresses).WithRequired(x => x.Account);
    }
}

public class EmailAddress
{
    [Key]
    public string Email { get; set; }

    public EmailTypes Type { get; set; }

    public long AccountID { get; set; }
    public virtual Account Account { get; set; }
}

public class EmailAddressConfiguration : EntityTypeConfiguration<EmailAddress>
{
    public EmailAddressConfiguration()
    {
        HasKey(x => x.Email);
        HasRequired(x => x.Account).WithMany(x => x.EmailAddresses).HasForeignKey(x => x.AccountID);
    }
}

And here is my Entity Class:

public class MyEntities : DbContext
{
    public MyEntities()
    {
        Database.SetInitializer<MyEntities>(new DropCreateDatabaseAlways<MyEntities>());
    }

    public DbSet<User> Users { get; set; }
    public DbSet<Account> Accounts { get; set; }
    public DbSet<EmailAddress> EmailAddresses { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Configurations.Add(new UserConfiguration());
        modelBuilder.Configurations.Add(new AccountConfiguration());
        modelBuilder.Configurations.Add(new EmailAddressConfiguration());

        base.OnModelCreating(modelBuilder);
    }
}

And finally my code that runs in a test console application:

static void Main(string[] args)
    {
        var id = CreateUser();

        using (MyEntities db = new MyEntities())
        {
            var a = db.Users.Find(id);
            var b = a.Account.EmailAddresses;
            var c = db.Accounts.Find(id);
            var d = db.EmailAddresses.Where(x => x.Account.AccountID == id).ToList();
        }

    }

    private static long CreateUser()
    {
        using (MyEntities db = new MyEntities())
        {
            var u = new User();
            u.DisplayName = "TEST";

            u.Location.CountryCode = "US";
            u.Location.StateCode = "HI";
            u.Location.City = "Kauai";

            EmailAddress e = new EmailAddress();
            e.Email = DateTime.UtcNow.Ticks + "@microsoft.com";
            e.Type = EmailTypes.Current;

            u.Account.EmailAddresses.Add(e);

            db.Users.Add(u);

            var cnt = db.SaveChanges();
            // Here I get a return of the 4 entities saved, and my model looks correct.

            return u.UserID;
        }
    }

Once the model was saved (CreateUser), I was able to navigate the model and everything looked perfect.

The issue arises when I try to pull the data back out.
My variables:

a — navigating to email adderess shows 0 records.

b — this too shows 0 records in the collection.

c — navigating to email adderess shows 0 records.

d — here I can get email addresses (but not by navigating the model)

  • 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-31T10:02:02+00:00Added an answer on May 31, 2026 at 10:02 am

    Your test code to access the navigation properties relies on lazy loading. But your Account.EmailAddresses collection is not marked as virtual:

    public ICollection<EmailAddress> EmailAddresses { get; set; }
    

    Navigation properties must be virtual (like your User.Account property) in order to make lazy loading possible.

    As a side note: I recommend to remove the instantiation of the Account navigation property…

    Account = new Account();
    

    …from the User constructor. This is a known source for trouble:

    What would cause the Entity Framework to save an unloaded (but lazy loadable) reference over existing data?

    EF 4.1 Code First: Why is EF not setting this navigation property?

    Instantiating Location is fine because it’s a complex type and not a navigation property.

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

Sidebar

Related Questions

The source class: public class Post { public long ID { get; set; }
Here's the relevant bit of the source code: class Dice { String name ;
Here is source code: @OneToOne(fetch = FetchType.LAZY) @Cascade({SAVE_UPDATE, EVICT, DELETE}) @JoinColumn(name = A_ID, nullable
you can view full source code here dpaste.com/hold/167199 Error: delete() takes exactly 2 arguments
I have an application I need to analyze. I have the source code here.
I am trying to install MySQLdb package. I found the source code here .
I'm building a python application from some source code I've found Here I've managed
Edit: You can get the full source here: http://pastebin.com/m26693 Edit again: I added some
I have a new converted MVC2 project running against the MVC2 source code. I
Trying to get UpdateModel to work for my User. The User class has basic

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.