0

I have two models:

public class Customer : People
  {
     public int CustomerID { get; set; }
     public decimal? Weight { get; set; }
     public decimal? Height { get; set; }
     public ICollection<Purchase> Cart { get; set; } 
  }

and

public class Purchase
  {
     public int PurchaseID { get; set; }
     public DateTime Time { get; set; }
     public decimal TotalPrice { get; set; }
     public int Amount { get; set; }
     public Good Good { get; set; }
   }

I already have a customer and need to update his Cart (to add smth to it for example).

I've tried to do smth like this but this do nothing. What am i doing wrong?

Customer customer = new Customer()
      {
        CustomerID = currentID.Value                   
      };
var cart = new List<Purchase>();
      cart.Add(new Purchase()
      {
        Amount = 1,
        Good = good,
        Time = DateTime.Now,
        TotalPrice = good.Price    
      });                                                                                
      customer.Cart = cart;
      var entry = _context.Entry(customer);
      _context.Update(customer);
      _context.SaveChanges();

UPDATE:

I've tried to do suggested things but.. What i don't understand right in my life? Context when i try to update vs. Context when i try to view updated Customer

Sergey
  • 111
  • 8
  • `var entry = _context.Entry(customer);` Just for curiosity , Where are you using entry variable ? – Mayank Pathak Oct 06 '16 at 02:46
  • I've tried to follow [this guide](http://stackoverflow.com/a/15339512/5380012), but got an exception at `entry.Property(e => e.Cart).IsModified = true;` that is why i have deleted this line and forgot to add to a question – Sergey Oct 06 '16 at 05:40

1 Answers1

0

As you are trying to update your entity. So this is how i follow and it works.

 context.Entry(customer).State = System.Data.EntityState.Modified;
 context.ChangeTracker.DetectChanges();
 context.SaveChanges();
Mayank Pathak
  • 3,621
  • 5
  • 39
  • 67
  • Thank you but it seemed not working. I think the problem is that i try to write a Collection – Sergey Oct 06 '16 at 06:59