1

I'm trying to load something from the database in visual studio, which works. However, the object I'm getting has some variables that are null:

I'm loading an instance of class 'ClauseComponent'. This object has 2 properties of type 'ClauseComponent'. These 2 properties are null.

Screenshot:

Screenshot of the variables while debugging

Code from the repository:

Grade gr = grades.Include(l => l.DeterminateTableProp.ClauseComponent)
    .FirstOrDefault(g => g.GradeId == gradeId);

Code from the ClauseComponent mapper:

public ClauseComponentsMapper()
{
    ToTable("ClauseComponents");

    // Primary key
    HasKey(c => c.ClauseComponentId);

    // Properties
    Property(c => c.ClauseComponentId)
        .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);

}

Code from the Clause class (that is inheriting from the ClauseComponent class):

public virtual ClauseComponent YesClause { get; set; }
        public virtual ClauseComponent NoClause { get; set; }
        public String Name { get; private set; }

        public virtual Parameter Par1 { get; set; }
        public virtual Parameter Par2 { get; set; }
        public int Waarde;

        public Clause(String name, Parameter par1, int waarde)
        {
            this.Name = name;
            this.Par1 = par1;
            this.Waarde = waarde;
        }

        public Clause(String name, Parameter par1, Parameter par2)
        {
            this.Name = name;
            this.Par1 = par1;
            this.Par2 = par2;
        }

        public Clause()
        {

        }

What is causing this?

6
  • Null values in the database? Commented Mar 9, 2015 at 20:42
  • Nope, as you can see here: i.imgur.com/9HV9A6r.jpg Commented Mar 9, 2015 at 20:49
  • Is this Entity Framework? Debug it or use Wireshark to see what query is actually being executed. Commented Mar 9, 2015 at 20:59
  • Yes this is entity framework. I will asap post the query. Not at pc now. Commented Mar 9, 2015 at 21:02
  • {SELECT [Extent1].[GradeId] AS [GradeId], [Extent1].[name] AS [name], [Extent1].[DeterminateTableProp_DeterminateTableId] AS [DeterminateTableProp_DeterminateTableId] FROM [dbo].[Grades] AS [Extent1]} Commented Mar 9, 2015 at 21:52

2 Answers 2

0

Well, Unfortunately I don't think EF can handle eager loading on properties only referent to sub-classes (TPH).

There are workarounds using projections, like here but they're not pretty.

Sign up to request clarification or add additional context in comments.

3 Comments

I should do this with lazy loading, but how?
Try to understand this workaround on the same issue here
I dont think its the same issue.
0

The answer was that I needed to disable lazy loading in the ProjectContext class:

//this.Configuration.ProxyCreationEnabled = false;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.