Entity Framework Won't Include() all Tables

I've got a simple set of 3 tables that EF knows are related...

In my repository I execute the following...

var result = localContext.LexiconTerms.Include(i=>i.Locale).Include(i=>i.Lexicon)

When I examine the results Lexicon is always populated and Locale is always null

Now, if I look at the SQL generated I see this ...

SELECT 
    [Extent1].[Id] AS [Id], 
    [Extent1].[LexiconId] AS [LexiconId], 
    [Extent1].[ResourceId] AS [ResourceId], 
    [Extent1].[Value] AS [Value], 
    [Extent1].[LocaleId] AS [LocaleId], 
    [Extent1].[ResourceSet] AS [ResourceSet], 
    [Extent1].[Type] AS [Type], 
    [Extent1].[BinFile] AS [BinFile], 
    [Extent1].[TextFile] AS [TextFile], 
    [Extent1].[Filename] AS [Filename], 
    [Extent1].[Comment] AS [Comment], 
    [Extent1].[ValueType] AS [ValueType], 
    [Extent1].[Updated] AS [Updated], 
    [Extent2].[Name] AS [Name], 
    [Extent2].[LocaleCode] AS [LocaleCode], 
    [Extent3].[Id] AS [Id1], 
    [Extent3].[Name] AS [Name1], 
    [Extent3].[Description] AS [Description], 
    [Extent3].[DeletedOn] AS [DeletedOn]
    FROM   [Locale].[LexiconTerms] AS [Extent1]
    INNER JOIN [Locale].[Locale] AS [Extent2] ON [Extent1].[LocaleId] = [Extent2].[LocaleCode]
    INNER JOIN [Locale].[Lexicon] AS [Extent3] ON [Extent1].[LexiconId] = [Extent3].[Id]
    WHERE (N'Test1' = [Extent1].[ResourceId]) AND (N'UnitTest' = [Extent1].[ResourceSet]) AND (-2 = [Extent1].[LexiconId]) AND ([Extent1].[LocaleId] = @p__linq__0)

Which clearly demonstrates that EF is fetching the data. Further, running the SQL in SSMS, I can see all the appropriate values for all 3 of the tables...

So, what am I doing/not doing that is preventing EF populating the one related object and not the other?

I know I can get over this issue by, for example, creating a view, but I'm trying to understand what it is that EF is doing.

EDIT: I'm using Database First in EF. These are the classes that EF has generated...

public partial class Lexicon
{
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
    public Lexicon()
    {
        this.LexiconTerms = new HashSet<LexiconTerm>();
    }

    public int Id { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public Nullable<System.DateTime> DeletedOn { get; set; }

    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<LexiconTerm> LexiconTerms { get; set; }
}

public partial class LexiconTerm
{
    public int Id { get; set; }
    public int LexiconId { get; set; }
    public string ResourceId { get; set; }
    public string Value { get; set; }
    public string LocaleId { get; set; }
    public string ResourceSet { get; set; }
    public string Type { get; set; }
    public byte[] BinFile { get; set; }
    public string TextFile { get; set; }
    public string Filename { get; set; }
    public string Comment { get; set; }
    public int ValueType { get; set; }
    public Nullable<System.DateTime> Updated { get; set; }

    public virtual Lexicon Lexicon { get; set; }
    public virtual Locale Locale { get; set; }
}

public partial class Locale
{
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
    public Locale()
    {
        this.LexiconTerms = new HashSet<LexiconTerm>();
    }

    public string Name { get; set; }
    public string LocaleCode { get; set; }

    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<LexiconTerm> LexiconTerms { get; set; }
}

Answers


You use eagerly loading so You need to change your code like this.

var result = localContext.LexiconTerms.Include(i=>i.Locale.Lexicon) 

OR

var result = localContext.LexiconTerms.Include("Locale.Lexicon") 

It is useful document

https://msdn.microsoft.com/en-us/data/jj574232.aspx


Need Your Help

Java function / api for version checking programs

java version

I've tried to google the answer but I keep getting results relating to version compatibility and checking with Java itself.