I have the following entities:
- BIEntities db = new BIEntities();
- public class SALES
- {
- public int IDs {get; set;}
- public string product { get; set; }
- ICollection
COUNTRIES { get; set; } - }
- public class COUNTRIES
- {
- public int IDc { get; set; }
- public string country { get; set; }
- public virtual SALES SALES { get; set; }
- }
And a ViewModel:
- public class myViewModel
- {
- public int ID { get; set; }
- public string product { get; set; }
- List<string> country { get; set; }
- }
I am trying to update (edit) all entities from myViewModel:
- SALES sUpdate = db.SALES.FirstOrDefault(s => s.IDs == myModel.ID);
- if (sUpdate != null)
- {
- //This works fine!
- db.Entry(sUpdate).CurrentValues.SetValues(myModel);
- }
- var cUpdate = from c in db.COUNTRIES where c.IDc == myModel.ID select c;
- if (cUpdate != null)
- {
- foreach (var item in cUpdate)
- {
- item.country = myModel.country;
- item.IDc = myModel.ID;
- //What should I add here to update the records??
- }
- }
- db.SaveChanges();
This can update the main table "SALES" but table "COUNTRIES" is not updated.
Replies
Know the answer? Post it — somebody with the same question will find it here.