Entity Framework 6 Lazy Loading not working

北慕城南 提交于 2019-12-12 06:28:00

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/21951367/entity-framework-6-lazy-loading-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!