EF Core 6 - InvalidIncludePathError' to the 'ConfigureWarnings' method in 'DbContext.OnConfiguring' or 'AddDbContext'.

Cenk 1,001 Reputation points
2022-08-05T16:05:46.993+00:00

Hi,

I am getting this error;

'microsoft.entityframeworkcore.query.invalidincludepatherror': unable to find navigation 'vendors' specified in string based include path 'vendors'. this exception can be suppressed or logged by passing event id 'coreeventid.invalidincludepatherror' to the 'configurewarnings' method in 'dbcontext.onconfiguring' or 'adddbcontext'.  

at this line;

return await this._db.Orders.Include("OrderDetails").Include("Vendors").ToListAsync();  

Here are my related models;

public class Order  
    {  
        public int Id { get; set; }  
          
        [Required]  
        public DateTime OrderDateTime { get; set; }  
        [Required]  
        [MaxLength(250)]  
        public string CustomerName { get; set; }  
        public string Status { get; set; }  
        [MaxLength(50)]  
        public string DoneBy { get; set; }  
        public List<OrderDetail> OrderDetails { get; set; }  
  
    }  
  
public class OrderDetail  
    {  
        public int Id { get; set; }  
          
        [Required]  
        [MaxLength(100)]  
        public string ProductCode { get; set; }  
        [Required]  
        [MaxLength(250)]  
        public string ProductName { get; set; }  
        [Required]  
        public int BuyQuantity { get; set; }  
        [Required]  
        public int SellQuantity { get; set; }  
        public double CostRatio { get; set; }  
        public double UnitCost { get; set; }  
        public double TotalBuyPrice { get; set; }  
        public double TotalSellPrice { get; set; }  
        [MaxLength(150)]  
        public string ShippingNumber { get; set; }  
        public string Status { get; set; }  
        [MaxLength(150)]  
        public string TrackingNumber { get; set; }  
        [MaxLength(400)]  
        public string Description { get; set; }  
        public int OrderId { get; set; }  
        [Required]  
        [MaxLength(250)]  
        public int VendorId { get; set; }  
        public virtual Order Order { get; set; }  
        public virtual Vendor Vendor { get; set; }  
    }  
public class Vendor  
    {  
        public int Id { get; set; }  
        [Required]  
        public string Name { get; set; }  
        [Required]  
        public string Address { get; set; }  
        [Required]  
        [RegularExpression(@"^((?!\.)[\w-_.]*[^.])(@\w+)(\.\w+(\.\w+)?[^.\W])$", ErrorMessage = "Invalid email address.")]  
        public string Email { get; set; }  
        [Required]  
        public string PhoneNumber { get; set; }  
        [Required]  
        public string MainResponsibleName { get; set; }  
        public string AssistantResponsibleName { get; set; }  
    }  

How can I fix?

Thanks in advance.

Entity Framework Core
Entity Framework Core
A lightweight, extensible, open-source, and cross-platform version of the Entity Framework data access technology.
740 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. AgaveJoe 28,131 Reputation points
    2022-08-05T18:46:44.497+00:00

    You named the property "Vendor" not "Vendors".

    I recommend passing an expression rather than a string which prone to mistakes like typos.

    Please read the official documentation.
    Loading Related Data

    0 comments No comments

  2. Cenk 1,001 Reputation points
    2022-08-06T05:26:20.947+00:00

    @AgaveJoe thank you for your reply. I changed it to Vendor but still, have this same error.

    I think the second include expects Order but unfortunately, there is no relationship between Order and Vendor.

    return await this._db.Orders.Include("OrderDetails").Include("Vendor").ToListAsync();

    228639-vendor.png


  3. AgaveJoe 28,131 Reputation points
    2022-08-06T20:33:28.53+00:00

    I am getting this error if I use ThenInclude

    You did not share the source code. My best guess you did not follow my recommendations or the openly published documentation and are continuing to use a string to navigate relationships like below.

    return await this._db.Orders.Include("OrderDetails").ThenInclude("Vendor").ToListAsync();  
    

    The syntax should have the following pattern.

    return await this._db.Orders.Include(d => d.OrderDetails).ThenInclude(v => v.Vendor).ToListAsync();  
    

    I believe if you want to use a string to navigate relationships then you need to use dot syntax to drill into the relationships using Include(). You cannot add ThenInclude() to an Include("string") because it returns an IQueryable<T> not an IIncludableQueryable<T, TN>. The reference documentation is very very clear on this.

    The following tutorial has examples of ThenInclude() as well as many other best practices. Please go through the tutorial rather than guessing.

    Razor Pages with Entity Framework Core in ASP.NET Core - Tutorial 1 of 8

    Step 6 has the information but you should start at the beginning.

    Part 6, Razor Pages with EF Core in ASP.NET Core - Read Related Data

    To use strings to navigate relationships use the following pattern. I modified the tutorial source code (above) and verified the code functions as expected.

    Instructor instructors = await _context.Instructors  
        .Include("OfficeAssignment")  
        .Include("Courses.Department")  
        .FirstOrDefaultAsync(i => i.ID == 1);  
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.