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

raysefo

Well-known member
Joined
Feb 22, 2019
Messages
361
Programming Experience
10+
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 of code;
return await this._db.Orders.Include("OrderDetails").Include("Vendors").ToListAsync();

Here are my related models;
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.
 
Solution
It's not for me to explain how your source control system works. If you're using one, you need to learn how it works for yourself. If you aren't using one and you have no backup to revert to then you likely have no choice but to start over. I don't think that VS sends files to the recycle bin so your migration file is gone for good. Starting over doesn't mean creating a whole new project though. It just means creating new migrations. The first migration will now be as your project is now, rather than how it was initially and with additional migrations. You can clear your database and have it recreated from that one new migration.
Actually, I'm not sure that that method is even looking for a property on the OrderDetail class. If you use Include to get a child entity or entity collection and you then want a child of that child, I think you should be calling ThenInclude. I think that all Include calls refer to the root and ThenInclude calls refer to the previous Include or ThenInclude results.
 
I am getting this error if I use ThenInclude

Severity Code Description Project File Line Suppression State Error CS1061 'IQueryable<Order>' does not contain a definition for 'ThenInclude' and no accessible extension method 'ThenInclude' accepting a first argument of type 'IQueryable<Order>' could be found (are you missing a using directive or an assembly reference?) IMS.Plugins.EFCore C:\Users\197199\Source\Repos\IMS.WebApp\IMS.Plugins.EFCore\OrderRepository.cs 50 N/A

Packages.png
 
Using like this works;
return await _db.Orders.Include(d => d.OrderDetails).ThenInclude(v => v.Vendor).ToListAsync();

But there is an inconsistency when editing relationships between entities. I also deleted a migration, How can I fix this inconsistency? I expect your support.

Error: Microsoft.Data.SqlClient.SqlException (0x80131904): Invalid column name 'VendorId'. Invalid column name 'VendorId'.

snapshot:
snapshot:
using System;
 using IMS.Plugins.EFCore;
 using Microsoft.EntityFrameworkCore;
 using Microsoft.EntityFrameworkCore.Infrastructure;
 using Microsoft.EntityFrameworkCore.Metadata;
 using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
    
 #nullable disable
    
 namespace IMS.Plugins.EFCore.Migrations
 {
     [DbContext(typeof(IMSContext))]
     partial class IMSContextModelSnapshot : ModelSnapshot
     {
         protected override void BuildModel(ModelBuilder modelBuilder)
         {
 #pragma warning disable 612, 618
             modelBuilder
                 .HasAnnotation("ProductVersion", "6.0.7")
                 .HasAnnotation("Relational:MaxIdentifierLength", 128);
    
             SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder, 1L, 1);
    
             modelBuilder.Entity("IMS.CoreBusiness.Customer", b =>
                 {
                     b.Property<int>("CustomerId")
                         .ValueGeneratedOnAdd()
                         .HasColumnType("int");
    
                     SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("CustomerId"), 1L, 1);
    
                     b.Property<string>("Address")
                         .IsRequired()
                         .HasColumnType("nvarchar(max)");
    
                     b.Property<string>("AssistantResponsibleName")
                         .IsRequired()
                         .HasColumnType("nvarchar(max)");
    
                     b.Property<string>("DeliveryAddress")
                         .IsRequired()
                         .HasColumnType("nvarchar(max)");
    
                     b.Property<string>("Email")
                         .IsRequired()
                         .HasColumnType("nvarchar(max)");
    
                     b.Property<string>("MainResponsibleName")
                         .IsRequired()
                         .HasColumnType("nvarchar(max)");
    
                     b.Property<string>("Name")
                         .IsRequired()
                         .HasColumnType("nvarchar(max)");
    
                     b.Property<string>("PhoneNumber")
                         .IsRequired()
                         .HasColumnType("nvarchar(max)");
    
                     b.Property<string>("TaxAdministration")
                         .IsRequired()
                         .HasColumnType("nvarchar(max)");
    
                     b.Property<int>("TaxNumber")
                         .HasColumnType("int");
    
                     b.HasKey("CustomerId");
    
                     b.ToTable("Customers");
                 });
    
             modelBuilder.Entity("IMS.CoreBusiness.Order", b =>
                 {
                     b.Property<int>("Id")
                         .ValueGeneratedOnAdd()
                         .HasColumnType("int");
    
                     SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"), 1L, 1);
    
                     b.Property<string>("CustomerName")
                         .IsRequired()
                         .HasMaxLength(250)
                         .HasColumnType("nvarchar(250)");
    
                     b.Property<string>("DoneBy")
                         .IsRequired()
                         .HasMaxLength(50)
                         .HasColumnType("nvarchar(50)");
    
                     b.Property<DateTime>("OrderDateTime")
                         .HasColumnType("datetime2");
    
                     b.Property<string>("Status")
                         .IsRequired()
                         .HasColumnType("nvarchar(max)");
    
                     b.HasKey("Id");
    
                     b.ToTable("Orders");
                 });
    
             modelBuilder.Entity("IMS.CoreBusiness.OrderDetail", b =>
                 {
                     b.Property<int>("Id")
                         .ValueGeneratedOnAdd()
                         .HasColumnType("int");
    
                     SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"), 1L, 1);
    
                     b.Property<int>("BuyQuantity")
                         .HasColumnType("int");
    
                     b.Property<double>("CostRatio")
                         .HasColumnType("float");
    
                     b.Property<string>("Description")
                         .IsRequired()
                         .HasMaxLength(400)
                         .HasColumnType("nvarchar(400)");
    
                     b.Property<int>("OrderId")
                         .HasColumnType("int");
    
                     b.Property<string>("ProductCode")
                         .IsRequired()
                         .HasMaxLength(100)
                         .HasColumnType("nvarchar(100)");
    
                     b.Property<string>("ProductName")
                         .IsRequired()
                         .HasMaxLength(250)
                         .HasColumnType("nvarchar(250)");
    
                     b.Property<int>("SellQuantity")
                         .HasColumnType("int");
    
                     b.Property<string>("ShippingNumber")
                         .IsRequired()
                         .HasMaxLength(150)
                         .HasColumnType("nvarchar(150)");
    
                     b.Property<string>("Status")
                         .IsRequired()
                         .HasColumnType("nvarchar(max)");
    
                     b.Property<double>("TotalBuyPrice")
                         .HasColumnType("float");
    
                     b.Property<double>("TotalSellPrice")
                         .HasColumnType("float");
    
                     b.Property<string>("TrackingNumber")
                         .IsRequired()
                         .HasMaxLength(150)
                         .HasColumnType("nvarchar(150)");
    
                     b.Property<double>("UnitCost")
                         .HasColumnType("float");
    
                     b.Property<int>("VendorId")
                         .HasColumnType("int");
    
                     b.HasKey("Id");
    
                     b.HasIndex("OrderId");
    
                     b.HasIndex("VendorId");
    
                     b.ToTable("OrdersDetail");
                 });
    
             modelBuilder.Entity("IMS.CoreBusiness.Vendor", b =>
                 {
                     b.Property<int>("Id")
                         .ValueGeneratedOnAdd()
                         .HasColumnType("int");
    
                     SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"), 1L, 1);
    
                     b.Property<string>("Address")
                         .IsRequired()
                         .HasColumnType("nvarchar(max)");
    
                     b.Property<string>("AssistantResponsibleName")
                         .IsRequired()
                         .HasColumnType("nvarchar(max)");
    
                     b.Property<string>("Email")
                         .IsRequired()
                         .HasColumnType("nvarchar(max)");
    
                     b.Property<string>("MainResponsibleName")
                         .IsRequired()
                         .HasColumnType("nvarchar(max)");
    
                     b.Property<string>("Name")
                         .IsRequired()
                         .HasColumnType("nvarchar(max)");
    
                     b.Property<string>("PhoneNumber")
                         .IsRequired()
                         .HasColumnType("nvarchar(max)");
    
                     b.HasKey("Id");
    
                     b.ToTable("Vendors");
                 });
    
                
             modelBuilder.Entity("IMS.CoreBusiness.OrderDetail", b =>
                 {
                     b.HasOne("IMS.CoreBusiness.Order", "Order")
                         .WithMany("OrderDetails")
                         .HasForeignKey("OrderId")
                         .OnDelete(DeleteBehavior.Cascade)
                         .IsRequired();
    
                     b.HasOne("IMS.CoreBusiness.Vendor", "Vendor")
                         .WithMany("OrderDetails")
                         .HasForeignKey("VendorId")
                         .OnDelete(DeleteBehavior.Cascade)
                         .IsRequired();
    
                     b.Navigation("Order");
    
                     b.Navigation("Vendor");
                 });
    
                
    
             modelBuilder.Entity("IMS.CoreBusiness.Order", b =>
                 {
                     b.Navigation("OrderDetails");
                 });
    
              
    
             modelBuilder.Entity("IMS.CoreBusiness.Vendor", b =>
                 {
                     b.Navigation("OrderDetails");
                 });
 #pragma warning restore 612, 618
         }
     }
 }

Tables.png
 
Firstly, this code demonstrates that you don't understand the entities you're dealing with:
C#:
return await _db.Orders.Include(d => d.OrderDetails).ThenInclude(v => v.Vendor).ToListAsync();
Your `d` variable is type Order, not OrderDetail, and your v variable is type OrderDetail, not Vendor. If you want to write code that isn't misleading then it should be:
C#:
return await _db.Orders.Include(o => o.OrderDetails).ThenInclude(od => od.Vendor).ToListAsync();
 
I also deleted a migration
What exactly does this mean? Please tell me you didn't just select it in the Solution Explorer and delete the file. Never do that. You should remove a migration in pretty much the same way as you add one. I use the Package Manager Console, but there are other ways. The command add-migration MigrationName adds a new one and remove-migration removes the last one added. That last command will update the model snapshot so, if that's where your issue is, that's probably why there's an issue. In that case you should either roll back to the last good commit in source control or else blow away your whole migrations folder and start again.
 
Please tell me you didn't just select it in the Solution Explorer and delete the file
I added migration, update the database then, and deleted it from solution explorer. How to roll back to the last good commit? I don't want to start all over :)
 
It's not for me to explain how your source control system works. If you're using one, you need to learn how it works for yourself. If you aren't using one and you have no backup to revert to then you likely have no choice but to start over. I don't think that VS sends files to the recycle bin so your migration file is gone for good. Starting over doesn't mean creating a whole new project though. It just means creating new migrations. The first migration will now be as your project is now, rather than how it was initially and with additional migrations. You can clear your database and have it recreated from that one new migration.
 
Solution
Back
Top Bottom