英文:
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<WarehouseBatchContent> warehouseBatchContentRepo, IUnitOfWork uow, IGenericRepository<WarehouseBatch> 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<WarehouseBatch> 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<WarehouseBatchContent> WarehouseBatchContents { get; set; } = new List<WarehouseBatchContent>();
}
}
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<WarehouseBatchContent> _warehouseBatchContentRepo;
private readonly IGenericRepository<WarehouseBatch> _warehouseBatchRepo;
public ProductBatcher(IGenericRepository<WarehouseBatchContent> warehouseBatchContentRepo, IUnitOfWork uow, IGenericRepository<WarehouseBatch> 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:
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<WarehouseBatch> 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<WarehouseBatchContent> WarehouseBatchContents { get; set; } = new List<WarehouseBatchContent>();
}
}
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论