2

I have the following classes:

public class Invoice
{    
     [key]
     public Guid InvoiceID { get; set; }
     public string Name { get; set; }
     public List<Invoice_item> Invoice_items { get; set; }
     
     public Invoice()
     {
         InvoiceID = Guid.NewGuid();
         Invoice_items = new List<Invoice_item>();
     }
} 

and:

public class Invoice_item
{
     [Key]
     public Guid Invoice_itemID { get; set; }
     public string Description{ get; set; }
     public int Price { get; set; }
     public int qty { get; set; }
     
     public Invoice_item()
     {
            Invoice_itemID = Guid.NewGuid();
     }
}

It works fine. I can edit this model. Controller: ...

Invoice invoice = await _context.Invoice.Include(i => i.Invoice_items).Where(x => x.InvoiceID == id).FirstOrDefaultAsync();

invoice.Name = "2020/122";
invoice.Invoice_items[0].Description = "Some name";
invoice.Invoice_items[0].Price = 1;
invoice.Invoice_items[0].qty = 1;

_context.Update(invoice);
await _context.SaveChangesAsync();

// WORK OK

But the problem is when I want to add an item to the list: Controller: ...

Invoice invoice = await _context.Invoice.Include(i => i.Invoice_items).Where(x => x.InvoiceID == id).FirstOrDefaultAsync();

invoice.Name = "2020/122";
invoice.Invoice_items[0].Description = "Description 1";
invoice.Invoice_items[0].Price = 1;
invoice.Invoice_items[0].qty 1;

Invoice_item item = new Invoice_item()
{
  Description = "Description 2",
  Price = 2,
  qty = 2
};

invoice.Invoice_items.Add(item);   // PROBLEM
                       
_context.Update(invoice);
await _context.SaveChangesAsync();

When editing the "Invoice" record, it is possible to edit all elements of the record and the elements of the "Invoice_item" list attached to it. The problem occurs when I want to add an item to the "Invoice_item" list attached to the "Invoice" record.

Please help.

6
  • what is the error you get? can you provide any details on the exceptions? Commented Dec 14, 2020 at 19:32
  • DbUpdateConcurrencyException: Database operation expected to affect 1 row(s) but actually affected 0 row(s) Commented Dec 14, 2020 at 19:37
  • This problem is about ChangeTracker, you can read on entityframeworktutorial.net/efcore/… link Commented Dec 14, 2020 at 19:43
  • Does your Invoice_item entity have a primary key set? Commented Dec 14, 2020 at 20:59
  • Yes. Guid Invoice_itemID is Key Commented Dec 14, 2020 at 21:33

1 Answer 1

1

You just need to add a Invoice_itemID in your item ,change your code like below.

Invoice_item item = new Invoice_item()
        {
            //add this line.
            Invoice_itemID = new Guid(),
            Description = "Description 2",
            Price = 2,
            qty = 2
        };
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.