Why am I getting "Microsoft.Data.SqlClient.SqlException: 'Invalid column name 'WarehouseBatchContentId'" When I am trying to get a WarehouseBatch?

huangapple go评论64阅读模式
英文:

Why am I getting "Microsoft.Data.SqlClient.SqlException: 'Invalid column name 'WarehouseBatchContentId'" When I am trying to get a WarehouseBatch?

问题

I keep getting this error message even though the table I am trying to GetById() (a method from my generic repository) from doesn't have a WarehouseBatchContentId in it. I am trying to get the WarehouseBatchId which is in the WarehouseBatch table as opposed to the WarehouseBatchContent table which does have a WarehouseBatchContentId in it.

我一直收到这个错误消息,尽管我正在尝试从中获取 GetById()(来自我的通用存储库的方法)的表中没有 WarehouseBatchContentId 的表。 我试图获取的是 WarehouseBatch 表中的 WarehouseBatchId,而不是 WarehouseBatchContent 表中确实有 WarehouseBatchContentId 的表。

I can't see where this mix-up has happened!

我看不出这个混淆发生在哪里!

Product batcher application:

产品批次应用程序:

public class ProductBatcher : IBatchProducts
{
private readonly IUnitOfWork _uow;
private readonly IGenericRepository<WarehouseBatchContent> _warehouseBatchContentRepo;
private readonly IGenericRepository<WarehouseBatch> _warehouseBatchRepo;

public ProductBatcher(IGenericRepository&lt;WarehouseBatchContent&gt; warehouseBatchContentRepo, IUnitOfWork uow, IGenericRepository&lt;WarehouseBatch&gt; warehouseBatch)
{
    _warehouseBatchContentRepo = warehouseBatchContentRepo;
    _uow = uow;
    _warehouseBatchRepo = warehouseBatch;
}

/*creating a method which takes all the parameters and is called in the program.cs file*/
public void BatchMover(int Id)
{
    Console.WriteLine(dto);

    var oldBatch = _warehouseBatchRepo.GetById(Id);

    if (oldBatch == null)
    {
        throw new Exception("No batch matching the input");
    }
}

}

WarehouseBatchContent domain:

WarehouseBatchContent 领域:

namespace Domain;

public class WarehouseBatchContent
{
protected WarehouseBatchContent()
{
WarehouseBatches = new List<WarehouseBatch>();
}

public WarehouseBatchContent(int warehouseBatchId, int manufactoringLotId, int quantity)
{
    WarehouseBatchId = warehouseBatchId;
    ManufactoringLotId= manufactoringLotId;
    Quantity= quantity;
}

public int WarehouseBatchContentId { get; private set; }
public int WarehouseBatchId { get; private set; }
public int ManufactoringLotId { get; private set; }
public int Quantity { get; private set; }

public virtual ManufactoringLot? ManufactoringLotNavigation { get; set; }
public virtual WarehouseBatch? WarehouseBatchNavigation { get; set; }

public virtual ICollection&lt;WarehouseBatch&gt; WarehouseBatches { get; protected set; }

}

WarehouseBatch domain:

WarehouseBatch 领域:

namespace Domain;

public class WarehouseBatch
{
public WarehouseBatch(int locationId)
{
LocationId = locationId;
}

public int WarehouseBatchId { get; private set; }
public int LocationId { get; private set; }

public virtual Location? LocationNavigation { get; set; }

public virtual ICollection&lt;WarehouseBatchContent&gt; WarehouseBatchContents { get; set; } = new List&lt;WarehouseBatchContent&gt;();
}

}

Any suggestions?

有任何建议吗?

I was expecting the GetById() to use the WarehouseBatchId as the ID parameter, not WarehouseBatchContentId.

我期望 GetById() 使用 WarehouseBatchId 作为 ID 参数,而不是 WarehouseBatchContentId

英文:

I keep getting this error message even though the table I am trying to GetById() (a method from my generic repository) from doesn't have a WarehouseBatchContentId in it. I am trying to get the WarehouseBatchId which is in the WarehouseBatch table as opposed to the WarehouseBatchContent table which does have a WarehouseBatchContentId in it.

I can't see where this mix up has happened!

Product batcher application:

public class ProductBatcher : IBatchProducts
{
    private readonly IUnitOfWork _uow;
    private readonly IGenericRepository&lt;WarehouseBatchContent&gt; _warehouseBatchContentRepo;
    private readonly IGenericRepository&lt;WarehouseBatch&gt; _warehouseBatchRepo;

    public ProductBatcher(IGenericRepository&lt;WarehouseBatchContent&gt; warehouseBatchContentRepo, IUnitOfWork uow, IGenericRepository&lt;WarehouseBatch&gt; warehouseBatch)
    {
        _warehouseBatchContentRepo = warehouseBatchContentRepo;
        _uow = uow;
        _warehouseBatchRepo = warehouseBatch;
    }

    /*creating a method which takes all the parameters and is called in the program.cs file*/
    public void BatchMover(int Id)
    {
        Console.WriteLine(dto);
        
        var oldBatch = _warehouseBatchRepo.GetById(Id);

        if (oldBatch == null)
        {
            throw new Exception(&quot;No batch matching the input&quot;);
        }
    }
}

WarehouseBatchContent domain:

namespace Domain;

public class WarehouseBatchContent
{
    protected WarehouseBatchContent()
    {
        WarehouseBatches = new List&lt;WarehouseBatch&gt;();
    }

    public WarehouseBatchContent(int warehouseBatchId, int manufactoringLotId, int quantity)
    {
        WarehouseBatchId = warehouseBatchId;
        ManufactoringLotId= manufactoringLotId;
        Quantity= quantity;
    }

    public int WarehouseBatchContentId { get; private set; }
    public int WarehouseBatchId { get; private set; }
    public int ManufactoringLotId { get; private set; }
    public int Quantity { get; private set; }

    public virtual ManufactoringLot? ManufactoringLotNavigation { get; set; }
    public virtual WarehouseBatch? WarehouseBatchNavigation { get; set; }

    public virtual ICollection&lt;WarehouseBatch&gt; WarehouseBatches { get; protected set; }
}

WarehouseBatch domain:

namespace Domain;

public class WarehouseBatch
{
    public WarehouseBatch(int locationId)
    {
        LocationId = locationId;
    }

    public int WarehouseBatchId { get; private set; }
    public int LocationId { get; private set; }

    public virtual Location? LocationNavigation { get; set; }

    public virtual ICollection&lt;WarehouseBatchContent&gt; WarehouseBatchContents { get; set; } = new List&lt;WarehouseBatchContent&gt;();
    }
}

Any suggestions?

I was expecting the GetById() to use the WarehouseBatchId as the ID parameter, not WarehouseBatchContentId.

答案1

得分: 1

解决:是因为这一行代码 public virtual ICollection<WarehouseBatchContent> WarehouseBatchContents { get; set; } = new List<WarehouseBatchContent>(); 并不代表该领域中的一对多关系。

英文:

Solved: it was because of this line public virtual ICollection<WarehouseBatchContent> WarehouseBatchContents { get; set; } = new List<WarehouseBatchContent>(); which is not represntative of the one to many relationship in that domain

答案2

得分: 0

[Key]
public int WarehouseBatchId { get; private set; }

尝试使用DataAnnotations库来设置键,就像这样
using System.ComponentModel.DataAnnotations

英文:
[Key]
public int WarehouseBatchId { get; private set; }

try setting key from dataAnnotations library like this
using System.ComponentModel.DataAnnotations

huangapple
  • 本文由 发表于 2023年4月19日 21:54:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/76055372.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定