Entity Framework not loading when using Include ()

I have a simple model and a simple query. I am trying to get EF to load my nav properties:

// Document object has a navigation property "DocType" of type DocType
// DocType object has a navigation property "Documents" of type ICollection<Documents> that is NOT virutal

context.Set<Document>().Where(d=>d.Id == id).Include(d=>d.DocType).SingleOrDefault();

      

The problem is that it doesn't actually cause stress DocType

. The unfamiliar is that the call exception Include()

does load the DocType property, but as a second request.

I looked around and applied all the fixes I found:

  • Added challenge Include()

  • Removed virtual

    from both navigation properties

Any idea what's going on here? Is it possible to force EF to combine this into a single request that is loaded?

EDIT: This is my data model:

namespace Data.Models {

    class Document {
        public int Id { get; set;}
        public int TypeId { get; set; }
        public DocType DocType { get; set; }
    }

    class DocType {
        public int Id { get; set; }
        public string FullName { get; set; }
        public ICollection<Document> Documents { get; set; }
    }
}

namespace Data.Mappings {

    internal class DocumentTypeConfiguration : EntityTypeConfiguration<Document> {

        public DocumentTypeConfiguration() {
            ToTable("ProsDocs");

            HasKey(m => m.Id);

            Property(m => m.Id)
                .HasColumnName("ProsDocId")
                .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);

            Property(m => m.TypeId)
                .HasColumnName("ProsDocTypeId")
                .HasMaxLength(3);

            HasRequired(d => d.DocType)
                .WithMany(dt=>dt.Documents)
                .WithForeignKey(d=>d.TypeId);
        }
    }

    internal class DocTypeTypeConfiguration : EntityTypeConfiguration<DocType> {

        public DocTypeTypeConfiguration() {
            ToTable("DocType");

            HasKey(m => m.Id);

            Property(m => m.Id)
                .HasColumnName("DocTypeId")
                .HasMaxLength(4);

            Property(m => m.FullName)
                .HasColumnName("DocTypeDesc")
                .HasMaxLength(255);
        }
    }
}

      

The strangest thing is that when I call:

context.Set<Document>().Find(id);

      

The properties DocType

get populated, but EF does it by making two separate requests. Can this be designed in such a way that EF understands that it can be done with a single request?

EDIT 2: This question appears to be about the same problem, but only indicates that the caller is Include()

fixing it, which doesn't work in my case.

+3


source to share


2 answers


In general, using a string instead of an expression (as @vanraidex mentioned) is not good practice. In general. However, when using third party providers (like Oracle Provider) this might be the only way to get correct sql (with joins).

So, if you are using a custom data provider and the .Include () method doesn't work, try using a string instead of an expression.



context.Documents.Where(d=>d.Id == id).Include("DocType").SingleOrDefault();

      

0


source


To include navigation properties, I use this syntax (with quotes in Include):



context.Documents.Where (d => d.Id == id). Include ("DocType") .SingleOrDefault ();

-1


source







All Articles