none
Update ICollection in Entity (Entity Framework 6, ASP.NET Core) RRS feed

  • Question

  • 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();

    Wednesday, October 5, 2016 11:32 PM

All replies

  • In EF6 (not sure if it's the same with EF Core) you would do the following to update a record.

    _context.Entry(customer).State = EntityState.Modified; 
    _context.SaveChanges(); 


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Thursday, October 6, 2016 1:47 AM
  • Hi Piteryo7,

    Since your problem is more related to entity framework. I will move it to ADO.NET Entity Framework and LINQ to Entities forum for support.

    The Visual C# forum discuss and ask questions about the C# programming language, IDE, libraries, samples, and tools.

    If you have some grammar or code errors, please feel free to contact us. We will try our best to give you a solution.

    Thanks for your understanding and cooperation.

    Best Regards,

    Wendy


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Thursday, October 6, 2016 1:58 AM
  • Hi Piteryo7,

    According to your description and related code, it seems that it's a on-to-many relationship, and you want to update main entity with adding sub entities. please refer to the following code.

    using (var _context = new Model1())
                {
                    Customer customer = new Customer()
                    {
                        CustomerID = 1
                    };
                    var cart = new List<Purchase>();
                    cart.Add(new Purchase()
                    {
                        Amount = 1,
                        //Good = good,
                        Time = DateTime.Now,
                        TotalPrice = 100
                    });
                    cart.ToList<Purchase>().ForEach(tchr => _context.Entry(tchr).State = EntityState.Added);
                    customer.Cart = cart;
                    var entry = _context.Entry(customer);
                    _context.SaveChanges();
                }

    Best regards,

    Cole Wu


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, October 6, 2016 2:40 AM
    Moderator
  • Thank you for your answer! It seemed not working too.. Am i right that just make a new List and try to write it to Entity?
    Thursday, October 6, 2016 6:58 AM
  • Thank you for your answer! It seemed not working too.. Am i right that just make a new List and try to write it to Entity?

    I should had shown

    _context.Entry(customer).State = EntityState.Added; 


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Thursday, October 6, 2016 10:16 AM