0

I want to seed data from ProformaInvoice table to BTBPending table. What I did everything is right but in ProformaInvoice table it is being reference with ItemBreakDown& StyleBreakDown tables using foreign key.

The problem is when I send data from obj parameter to BTBPending table I can trigger data which comes from ItemBreakDown & StyleBreakDown.

What I did is shown here - ProformaInvoice controller class, Create method:

public IActionResult Create(ProformaInvoice obj)
{
        if (ModelState.IsValid)
        {
            _unitOfWork.ProformaInvoice.Add(obj);
            _unitOfWork.Save();

            BTBPending objBTBPendingList = new BTBPending();

            objBTBPendingList.PendingId = obj.PiId;
            objBTBPendingList.SupplierListId = obj.SupplierListId;
            objBTBPendingList.CountryListId = obj.CountryListId;
            objBTBPendingList.ItemListId = obj.ItemBreakDownList;
            objBTBPendingList.PiValue = obj.PiValue;
            objBTBPendingList.PINo = obj.PINo;
            objBTBPendingList.TradeTermListId = obj.TradeTermListId;
            objBTBPendingList.BuyerListId = obj.BuyerListId;
            objBTBPendingList.StyleListId = obj.StyleListId; 

            _unitOfWork.BTBPending.Add(objBTBPendingList);
            _unitOfWork.Save();

            return CreatedAtAction("GetDetails", new { id = obj.PiId }, obj);
        }

        _logger.LogError($"Something went wrong in the {nameof(Create)}");
        return StatusCode(500, "Internal Server Error, Please Try Again Later!");
}

Problem is here - I can't access ItemListId which is store in the ItemBreakDown table:

objBTBPendingList.ItemListId = obj.ItemBreakDownList; 

And on this line also:

objBTBPendingList.StyleListId = obj.StyleListId 

ProformaInvoice model class :

public class ProformaInvoice
{
    [Key]
    public int PiId { get; set; }
    public int? ActualContractId { get; set; }
    //[ForeignKey("ContractListId")]
    public ContractList? ActualContract { get; set; }
    public int? ContractListId { get; set; }
    [ForeignKey("ContractListId")]
    [ValidateNever]
    public ContractList? ContractList { get; set; }
    public int? SupplierListId { get; set; }
    [ForeignKey("SupplierListId")]
    [ValidateNever]
    public SupplierList? SupplierList { get; set; }
    [DisplayName("COUNTRY")]
    public int? CountryListId { get; set; }
    [ForeignKey("CountryListId")]
    [ValidateNever]
    public CountryList? CountryList { get; set; }
    [DisplayName("VALUE")]
    [DataType(DataType.Currency)]
    [DisplayFormat(DataFormatString = "{0:C}", ApplyFormatInEditMode = false)]
    public decimal? PiValue { get; set; }
    [DisplayName("P/I No.")]
    public string? PINo { get; set; }
    [DisplayName("BUYER")]
    public int? BuyerListId { get; set; }
    [ForeignKey("BuyerListId")]
    [ValidateNever]
    public BuyerList? BuyerList { get; set; }
    [DisplayName("TRADE TERM")]
    public int? TradeTermListId { get; set; }
    [ForeignKey("TradeTermListId")]
    [ValidateNever]
    public TradeTermList? TradeTermList { get; set; }

    public string? LcNo { get; set; }
    public string? PiStatus { get; set; }

    public virtual List<ItemBreakDown> ItemBreakDownList { get; set; } = new List<ItemBreakDown>();
    public virtual List<StyleBreakDown> StyleBreakDownList { get; set; } = new List<StyleBreakDown>();
}

Please help me solve this issue. Thank you.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • Friendly advice: don't use "list" in names of objects or properties that aren't collections. It's very confusing. Your code doesn't show the type of `objBTBPendingList.ItemListId` so it's hard to understand the problem. – Gert Arnold Dec 31 '22 at 16:10

0 Answers0