我正在尝试通过 MaterialReturnNoteHeader 在 StockDetails 中添加详细信息但是它显示以下错误

问题描述 投票:0回答:0

我提供了以下代码以及它显示的错误。

public record AddMaterialReturnNoteCommand : ICreateCommand<AddMaterialReturnNoteCommand.AddMaterialReturnNoteModel, bool>
    {
        public AddMaterialReturnNoteModel Model { get; init; } = default!;
        public record AddMaterialReturnNoteModel(Guid VendorId,
                                                 DateTime Date,
                                                 IList<MaterialIReturnNoteDetailsModel> MaterialReturnNoteDetail);

        public record MaterialIReturnNoteDetailsModel(Guid ProductId,
                                                      float Quantity);

        internal class Validator : AbstractValidator<AddMaterialReturnNoteCommand>
        {
            public Validator()
            {
                RuleFor(r => r.Model.VendorId).NotEmpty().WithMessage("VendorId is required.");
                RuleFor(r => r.Model.Date).NotEmpty().WithMessage("Date is required.");
                RuleFor(r => r.Model.MaterialReturnNoteDetail).NotEmpty().WithMessage("Details are required.");
            }
        }
        public class Handler : IRequestHandler<AddMaterialReturnNoteCommand, ResultModel<bool>>
        {
            private readonly IRepository<MaterialReturnNoteHeader> _materialReturnNoteHeaderRepository;
            private readonly IRepository<MaterialReturnNoteDetails> _materialReturnNoteDetailsRepository;
            private readonly IRepository<StockDetails> _stockDetailsRepository;

            public Handler(IRepository<MaterialReturnNoteHeader> materialReturnNoteHeaderRepository, IRepository<MaterialReturnNoteDetails> materialReturnNoteDetailsRepository, IRepository<StockDetails> stockDetailsRepository)
            {
                _materialReturnNoteHeaderRepository = materialReturnNoteHeaderRepository ?? throw new ArgumentNullException(nameof(materialReturnNoteHeaderRepository));
                _materialReturnNoteDetailsRepository = materialReturnNoteDetailsRepository ?? throw new ArgumentNullException(nameof(materialReturnNoteDetailsRepository));
                _stockDetailsRepository = stockDetailsRepository ?? throw new ArgumentException(nameof(stockDetailsRepository));
            }

            public async Task<ResultModel<bool>> Handle(AddMaterialReturnNoteCommand request, CancellationToken cancellationToken)
            {
                if (request == null) throw new ArgumentNullException();
                var materialReturnNoteHeader = new MaterialReturnNoteHeader
                {
                    VendorId = request.Model.VendorId,
                    Date = request.Model.Date
                };

                var response = await _materialReturnNoteHeaderRepository.AddAsync(materialReturnNoteHeader);

                foreach (var item in request.Model.MaterialReturnNoteDetail)
                {
                    var result = await _materialReturnNoteDetailsRepository.AddAsync(MaterialReturnNoteDetails.Create(materialReturnNoteHeader.Id,
                                                                                                                      item.ProductId,
                                                                                                                      item.Quantity));
                    response.MaterialReturnNoteDetails.Add(result);

                    var stockDetails = new StockDetails
                    {
                        RefId = materialReturnNoteHeader.Id,
                        ProductId = item.ProductId,
                        Quantity = item.Quantity * -1,
                        Type = TypeEnum.Return
                    };
                    await _stockDetailsRepository.AddAsync(stockDetails);

                }
                return ResultModel<bool>.Create(true);
            }


        }
    }

这是它显示的错误

Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware[1]
server-apis-posservice.api-1   |       An unhandled exception has occurred while executing the request.
server-apis-posservice.api-1   |       Microsoft.EntityFrameworkCore.DbUpdateException: An error occurred while saving the entity changes. See the inner exception for details.
server-apis-posservice.api-1   |        ---> Npgsql.PostgresException (0x80004005): 42703: column "MaterialReturnNoteHeaderId" of relation "StockDetails" does not exist

它说 MaterialReturnNoteHeaderId 列不存在,但我不希望我必须将 MaterialReturnNoteHeaderId 存储为 RefId 的列。

我的代码有问题吗,或者我是否需要更改任何内容以便我可以将 MaterialReturnNoteHeaderId 存储为 RefId 。有没有其他方法可以将 MaterialReturnNoteHeaderId 存储为 RefId

c# asp.net entity-framework asp.net-core asp.net-web-api
© www.soinside.com 2019 - 2024. All rights reserved.