0

I am having some trouble with EF6 lazy loading, code first to an existing database.

Here are the Entities that are giving me the issue, I have no idea why it is not working, everything I find online says it should be working.

public class User
{
    public long UserId { get; set; }
    public string Email { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public virtual ICollection<Token> Tokens { get; set; }
    public virtual ICollection<Business> Businesses { get; set; }

    public virtual ICollection<Candidate> Candidates { get; set; }
}

Here is the configuration mappings:

public class Token
{
    public long TokenId { get; set; }
    public long UserId { get; set; }
    public Guid TokenValue { get; set; }
    public DateTime ExpirationDate { get; set; }
    public virtual User User { get; set; }
}

    public TokenMap()
    {
        this.HasKey(t => t.TokenId);

        this.Property(t => t.TokenValue)
            .IsRequired();
        this.Property(t => t.ExpirationDate)
            .IsRequired();

        this.ToTable("Tokens");
        this.Property(t => t.TokenId).HasColumnName("TokenId");
        this.Property(t => t.UserId).HasColumnName("UserId");
        this.Property(t => t.TokenValue).HasColumnName("TokenValue");
        this.Property(t => t.ExpirationDate).HasColumnName("ExpirationDate");

        this.HasRequired(s => s.User)
            .WithMany(s=>s.Tokens)
            .HasForeignKey(s=>s.UserId);
    }


    public UserMap()
    {
        this.ToTable("Users");
        this.HasKey(t => t.UserId);
        this.Property(t => t.Email)
            .IsRequired();
        this.Property(t => t.FirstName)
            .IsRequired();
        this.Property(t => t.LastName)
            .IsRequired();

        this.HasMany(t => t.Businesses)
            .WithMany(set => set.Users)
            .Map(m =>
            {
                m.ToTable("BusinessUser");
                m.MapLeftKey("UserId");
                m.MapRightKey("BusinessId");
            });

        this.HasMany(s => s.Tokens)
            .WithRequired(s => s.User)
            .HasForeignKey(s => s.UserId);

        this.HasMany(s => s.Candidates)
            .WithOptional(s => s.User)
            .HasForeignKey(s => s.UserId);
    }

And here is a few snippets from the context:

    public DbSet<Token> Token { get; set; }
    public DbSet<User> User { get; set; }

protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {

        modelBuilder.Configurations.Add(new TokenMap()); 
        modelBuilder.Configurations.Add(new UserMap());

    }

Whenever I do a SingleOrDefault on the Tokens entity the result' user is null.

Any idea what I am doing wrong? All the data in the database is right, and UserId does have a value.

Here is a more elaborate example of calling:

I am implementing the repository patern.

In the class doing the call' constructor I have:

context = new Consolid8ContextProvider();
uow = new UnitOfWork(context);

And then uow.Tokens.First(u => u.ExpirationDate > DateTime.Now && u.TokenValue == token);

Tokens is my TokenRepository that exposes the Tokens entity, and First is a wrapper for FirstOrDefault.

This results in a token object with all of the properties set except for the User navigation property

17
  • Could you show code that get data from database? And where you look at user property. Are you sure that your context is not closed between this two events? Commented Feb 22, 2014 at 7:54
  • @KirillBestemyanov I did an update Commented Feb 22, 2014 at 8:06
  • Another note is that I know my repo pattern is correct because it works like a charm using database first design Commented Feb 22, 2014 at 8:07
  • Is it a many to many mapping? Does a user have the token you are looking at? Check the database :) Commented Feb 22, 2014 at 11:56
  • Is the Token that is returned by SingleOrDefault a dynamic proxy? You can check that in the debugger by looking at the object's type name. It should be a proxy, otherwise lazy loading won't work. Did you possibly set LazyLoadingEnabled or ProxyCreationEnabled to false? Commented Feb 22, 2014 at 15:20

1 Answer 1

3

So I was using BreezeJS and it overrides your context with it's own settings, part of which is to set LazyLoading and EnableProxiesCreation to false.

So if you want to do queries outside of breeze you either have to implement a different constructor for your breeze provider or setting it per query as Slauma has suggested in the comments of the question.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.