I have a BaseScript, in the Script there is a GetAll which not load related entities.
In the DbContext there is LazyLodingEnabled disabled (false).
Following some Code to understand my issue. In this example it doesn't load the user and messageViews when I want to get the messages by language.
BaseScript
public abstract class BaseScript<TEntity> where TEntity : EntityBase
{
public virtual IEnumerable<TEntity> GetAll()
{
using (EntityDbContext ctx = new EntityDbContext())
{
return ctx.Set<TEntity>().ToList();
}
}
}
MessageScript
public class MessageScript : BaseScript<Message>
{
public IEnumerable<Message> GetAllByLanguagePublic(string language)
{
return this.GetAllPublic().Where(x => x.Language == language).ToList();
}
}
Message entity:
public class Message : EntityBase
{
public string Text { get; set; }
public string Language { get; set; }
public virtual User User { get; set; }
public Guid UserId { get; set; }
public virtual ICollection<MessageView> MessageViews { get; set; }
}
I suppose your question is "Why aren't User
and MessageViews
loaded?" If that is the case, you are answering yourself. If you disable lazy loading, you have to load these properties explicitly, using Include
(for example):
Maybe in your BaseScript.GetAll
you want to return an IQueryable
instead of a IEnumerable
so the query will not be exectued yet (do not call ToList()
). Then, in your GetAllByLanguagePublic
method you could do this:
public class MessageScript : BaseScript<Message>
{
public IEnumerable<Message> GetAllByLanguagePublic(string language)
{
return this.GetAll()
.Include(x => x.User)
.Include(x => x.MessageViews)
.Where(x => x.Language == language).ToList();
}
}
Note that you need to include System.Data.Entity
to use Include
with lambda expression.