Home > front end >  I want to add 2 entities at once
I want to add 2 entities at once

Time:10-15

I want to add 2 entities at once. When I want to add an offer, let the offer detail be added at the same time. But let's add more than one detail to a quote. I can't do this. I will be glad if you help.

 public class Offer
{
    public int Id { get; set; }
    public int CompanyId { get; set; }
    public int CompanyContactId { get; set; }
    public int OfferNumber { get; set; }
    public string Annotations { get; set; }
    public string CommercialConditions { get; set; }
    public string TimeInformation { get; set; }
    public decimal ProfitRate { get; set; }

    public DateTime Date { get; set; }
    public DateTime ValidityDate { get; set; }  


    public Currency Currency { get; set; } 
    public Status Status { get; set; }


    //Navigation Property
    public virtual Company Company { get; set; }

    public virtual CompanyContact CompanyContact { get; set; }

    public virtual List<OfferDetail> OfferDetail { get; set; }



public class OfferDetail
{
    //public int Id { get; set; }
    public int OfferId { get; set; }

    public int RowNumber { get; set; }
    public int Quantity { get; set; }
    public string Description { get; set; }
    public decimal UnitPrice { get; set; }
    public decimal TotalPrice { get; set; }
    public string Definition { get; set; }
    public Boolean Optional { get; set; }
    public decimal UnitProfit { get; set; }

    public decimal UnitCost { get; set; }

    //public Currency Currency { get; set; }

    //Navigation Properties

    public Offer Offer { get; set; }

When I add the offer table to the database, the offer details are also added. But let another detail be added to a quote.

    public async Task<Result> AddOffer(OfferInfo offerInfo)
    {
        try
        {
            var vOffer = new Offer
            {
                
                Id = offerInfo.Id,
                CompanyId = offerInfo.CompanyId,
                CompanyContactId = offerInfo.CompanyContactId,
                OfferNumber = offerInfo.OfferNumber,
                Annotations = offerInfo.Annotations,
                CommercialConditions = offerInfo.CommercialConditions,
                TimeInformation = offerInfo.TimeInformation,
                ProfitRate = offerInfo.ProfitRate,
                Date = offerInfo.Date,
                ValidityDate = offerInfo.ValidityDate,
                
            }; _context.Offers.Add(vOffer);
            await _context.SaveChangesAsync();
            
            return Result.PrepareSuccess();
        }
        catch (Exception vEx)
        {
            return Result.PrepareFailure(vEx.Message);

        }
    }

I can add from the model I created here.

This way I want to add both. Thanks in advance.

CodePudding user response:

If you assign list of OfferDetail to Offer details entities will automatically be added to adequate table.

It should be something like this:

var vOffer = new Offer
{
   // set all your properties of offer here
  OfferDetails = new [] 
  {
     new OfferDetail {/*init details here*/}, 
     // you can add more of details objects here 
  } 
}

CodePudding user response:

Chicken and egg problem, but there is a solution if you define constructors in OfferDetail that require the Offer and then add themselves to the List<OfferDetail>.

public class Offer
{
    public Offer()
    {
        OfferDetail = new List<OfferDetail>();
    }

    //Navigation Property
    public List<OfferDetail> OfferDetail { get;  }

}
public class OfferDetail
{
    public OfferDetail(Offer offer)
    {
        Offer = offer;
        offer.OfferDetail.Add(this);
    }

    public int OfferId { get => Offer.Id; }

    //Navigation Properties

    public Offer Offer { get; }
}
  •  Tags:  
  • c#
  • Related