ArgumentException:必须是可还原的节点

时间:2019-08-12 07:31:29

标签: c# asp.net-core asp.net-core-webapi ef-core-3.0

我正在为公司创建一个ASP.NET Core Web API,以管理客户和合同(稍后将添加更多功能)。我一直在遵循这份出色的指南(https://www.freecodecamp.org/news/an-awesome-guide-on-how-to-build-restful-apis-with-asp-net-core-87b818123e28/)。

第一个表(“客户”)使用EF Core 3可以完美地查询。但是,当我尝试查询另一个表(“合同”)时,我得到了ArgumentException: must be reducible node异常。 (注意:每个“客户”可以有多个“合同”,每个“合同”都链接到一个,并且只有一个“客户”。)

我已经搜索了此问题,大多数解决方案建议将EF Core升级到更高版本。但是,我已经在运行EF Core v3(预览版7),这是目前的最新版本。这是EF Core中的错误,还是我做错了什么?

我使用的代码:

我有2个课程,其中1个Customer课程:

public class Customer
{
    // Properties
    public int ID { get; set; }
    public string Name { get; set; }
    public string Type { get; set; }
    public string Segment { get; set; }
    public string Email { get; set; }
    public string ContactPerson { get; set; }
    public bool SendReminders { get; set; } = true;
    public string Notes { get; set; }

    // Foreign Properties
    public IList<Contract> Contracts { get; set; } = new List<Contract>();

    // Logging Properties
    public DateTime CreatedAt { get; set; }
}

还有一个Contract类:

public class Contract
{
    // Properties
    public int ID { get; set; }
    public string Name { get; set; }
    public string Notes { get; set; }

    // Foreign Properties
    public int CustomerID { get; set; }
    public Customer Customer { get; set; }

    //Logging Properties
    public DateTime CreatedAt { get; set; }
}

I覆盖OnModelCreating的{​​{1}}方法:

DBContext

当我查询“合同”表以获取所有合同时,此方法称为:

protected override void OnModelCreating(ModelBuilder builder)
{
    base.OnModelCreating(builder);

    builder.Entity<Customer>().ToTable("Customer");
    builder.Entity<Customer>().HasKey(x => x.ID);
    builder.Entity<Customer>().Property(x => x.ID).IsRequired().ValueGeneratedOnAdd();
    builder.Entity<Customer>().Property(x => x.Name).IsRequired().HasMaxLength(50);
    builder.Entity<Customer>().Property(x => x.Type).HasMaxLength(25);
    builder.Entity<Customer>().Property(x => x.Segment).HasMaxLength(25);
    builder.Entity<Customer>().Property(x => x.ContactPerson).HasMaxLength(50);
    builder.Entity<Customer>().HasMany(x => x.Contracts).WithOne(y => y.Customer).HasForeignKey(y => y.CustomerID);

    builder.Entity<Customer>().HasData(
        new Customer { ID = 100, Name = "Cust1" }, // ID set manually due to in-memory provider
        new Customer { ID = 101, Name = "Cust2" }
    );

    builder.Entity<Contract>().ToTable("Contract");
    builder.Entity<Contract>().HasKey(x => x.ID);
    builder.Entity<Contract>().Property(x => x.ID).IsRequired().ValueGeneratedOnAdd();
    builder.Entity<Contract>().Property(x => x.Name).IsRequired().HasMaxLength(50);

    builder.Entity<Contract>().HasData(
        new Contract { ID = 100, Name = "DLA", CustomerID = 100, Notes = "Cust1 contract" },
        new Contract { ID = 101, Name = "DLA", CustomerID = 101, Notes = "Cust2 contract" }
    );
}

带注释的行显示了所有合同,但未将“合同”表与“客户”表连接在一起。因此,我为此使用LINQ的public async Task<IEnumerable<Contract>> ListAsync() { return await _context.Contracts.Include(c => c.Customer).ToListAsync(); //return await _context.Contracts.ToListAsync(); } 方法。但是,该行代码会引发Include异常。

我严格按照指南进行操作(但当然可以根据需要更改名称)。我是在做错什么,还是这只是EF Core 3中的错误?

谢谢。

0 个答案:

没有答案