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.'


Please 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; } 

Write an Article
If you log in, you will be notified when someone leaves a comment.

Other users would like to know if this solution helped you.

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.


© 2022 - ErnesTech - Privacy