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
.
2
Answers
Solved: it was because of this line public virtual ICollection WarehouseBatchContents { get; set; } = new List(); which is not represntative of the one to many relationship in that domain
try setting key from dataAnnotations library like this
using System.ComponentModel.DataAnnotations