Implementing a Many-To-Many Relationship with Ardalis Specifications -Disregard #781
Unanswered
VincentBrandNIU
asked this question in
Q&A
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hey all,
Recently picked up this template for learning purposes, I'm trying to implement a many to many relationship in conjuction with the Ardalis Specifications. I'm running into a data conversion issue when using the specifications and have tried finding resources online, I was hoping someone may have run into a similiar issue. Currently I have:
`public class BillOfMaterialDetail : AuditableEntity, IAggregateRoot
{
public BillOfMaterialDetail()
{
}`
`public class Product : AuditableEntity, IAggregateRoot
{
public string Name { get; private set; } = default!;
public string? Description { get; private set; }
public decimal SellingPrice { get; private set; }
public decimal CostToProduce { get; private set; }
public bool IsRaw { get; private set; } = false;
public Guid BrandId { get; private set; }
public virtual Brand Brand { get; private set; } = default!;
public virtual ICollection? BillOfMaterialDetailProducts { get; set; }
public virtual BillOfMaterial? BillOfMaterial { get; set; }
And the Joining model:
public class BillOfMaterialDetailProduct { public Guid? ProductId { get; set; } public Product? Product { get; set; } public Guid? BillOfMaterialDetailId { get; set; } public BillOfMaterialDetail? BillOfMaterialDetail { get; set; } }
My DTO object:
`public class BillOfMaterialDetailsDto : IDto
{
public Guid Id { get; set; }
public Guid BillOfMaterialId { get; set; }
public decimal Quantity { get; set; }
public decimal Yield { get; set; }
public ICollection? BillOfMaterialDetailProducts { get; set; }
}`
And request handler:
`public class GetBillOfMaterialDetailsByBillOfMaterialIdRequest : IRequest
{
public Guid BillOfMaterialId { get; set; }
public GetBillOfMaterialDetailsByBillOfMaterialIdRequest(Guid billOfMaterialId) => BillOfMaterialId = billOfMaterialId;
}
The error I'm running into is converting the BillOfMaterialDetailsByBillOfMaterialIdSpec into the BillOfMaterialDetailsByBillOfMaterialIdSpec. I have attempted to adjust models and duplicated the examples for another model that has a one to many relationship and it worked with no issues. I'm sure this issue is caused by my lack of knowledge, but I'm trying to fill the gaps. The actual exception I'm getting is "Unable to cast object of type 'SmallInventory.Application.Catalog.BillOfMaterialDetails.BillOfMaterialDetailsByBillOfMaterialIdSpec' to type 'Ardalis.Specification.ISpecification`2[SmallInventory.Domain.Catalog.BillOfMaterialDetail,SmallInventory.Application.Catalog.BillOfMaterialDetails.BillOfMaterialDetailsDto]'."
Any insight on to why this would be happening or what the actual issue is would be awesome.
Thanks in Advance.
Beta Was this translation helpful? Give feedback.
All reactions