Lazy加载不起作用

Lazy加载不起作用

本文介绍了实体框架6 Lazy加载不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



这是给我这个问题的实体,我不知道为什么它不工作,我在网上找到的所有内容都应该是正常工作。

  public class User 
{
public long UserId {get;组; }
public string Email {get;组; }
public string FirstName {get;组; }
public string LastName {get;组; }
public virtual ICollection< Token>令牌{get;组; }
public virtual ICollection< Business>企业{get;组; }

public virtual ICollection< Candidate>候选人{get;组;
}

以下是配置映射:

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

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) ;
}

这里是上下文中的一些片段:

  public DbSet< Token>令牌{get;组; } 
public DbSet< User>用户{get;组;

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{

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

}

每当我在令牌实体上执行SingleOrDefault时,用户为空。



任何想法我做错了什么?数据库中的所有数据都是正确的,UserId确实有一个值。



这是一个更详细的调用示例:



我正在执行存储库patern。



在进行调用'构造函数的类中,我有:

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

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



令牌是我的TokenRepository,它暴露了令牌实体,首先是FirstOrDefault的包装器。



这将导致一个令牌对象,除了用户导航属性

解决方案

所以我正在使用BreezeJS,它会用自己的设置覆盖你的上下文,其中一部分是将LazyLoading和EnableProxiesCreation设置为false。



所以如果你想在微风之外做查询,你或者必须为你的微风提供者实现一个不同的构造函数,或者按照Slauma在问题的注释中提出的每个查询来设置它。


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

解决方案

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.

这篇关于实体框架6 Lazy加载不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 14:27