The expression 'b.listOfFileRecords' is invalid inside an 'Include' operation, since it does not represent a property access


Question: System.InvalidOperationException: 'The expression 'b.listOfFileRecords' is invalid inside an 'Include' operation, since it does not represent a property access: 't => t.MyProperty'. To target navigations declared on derived types, use casting ('t => ((Derived)t).MyProperty') or the 'as' operator ('t => (t as Derived).MyProperty'). Collection navigation access can be filtered by composing Where, OrderBy(Descending), ThenBy(Descending), Skip or Take operations. For more information on including related data, see http://go.microsoft.com/fwlink/?LinkID=746393.'


Login to See the Rest of the Answer

Answer:
Lets look at why this error occurs, first the error message is telling you that the Model (POCO) you have defined has no proper navigation between the Entity you want to include from one Model Class.

Take a look in the example:

  public partial class MyModal
    {
        [Key]
        public int Id { get; set; }
        public string SomeProperty { get; set; }
      
         [NotMapped]
        //This is where the error is, there is no proper navigation between MyModal and MySubClass
        //For this navigation to work, you need MyModalId in the MySubClass Modal Class
        //Likewise, the Property has to exist in the Database as well. You have to know the
        // relationship between the two, is it one to one or one to many or many to many. All that matters.
        public MySubClass listOfFileRecords { get; set; } 

   }

Make sure that the Entity you want to include in the main entity has no property called [NoMapped], it should be required. If you have the [NotMapped]
on top of the property the Include directive will not work.
Make sure that the Entity you want included in the main Entity has a #Foreign Key present in the main entity. See the code below:

MyModalMain{
public int SubEntityForeignId get set //foregn key
SubentityForeign myProperty get set
}

SubentityForeign{
public int Id get set //Primary key

}

//Now you could do this
var m = context.MyModalMain.Include(a => a.myProperty) //This way EF Core know the primary key


//Or you could do it like this:
MyModalMain{
SubentityForeign myProperty get set
}

SubentityForeign{
public int Id get set //Primary key
public int MyModalMainId get set;
}

//Now you could do this
var m = context.MyModalMain.Include(a => a.myProperty) //This way EF Core know the primary key




Mark said:

If you still have the error even after following the instruction, remove the "[NotMapped]" directive and you won't have an error anymore.

Posted On: January 20, 2022 13:34:57 PM


For peering opportunity Autonomouse System Number: AS401345 Custom Software Development at ErnesTech Email Address[email protected]