linq - Update multiple entities in Entity Framework -


i trying update 2 tables situation , situationcategory, not updating mentioned in below code , image.

public async task<bool> updatesituation(int id, situationsdto data) {     situations result = _mapper.map<situationsdto, situations>(data);     result.deleted = true;      _context.entry(result).state = entitystate.modified;     await _context.savechangesasync();      situationcategories situationcategory = new situationcategories();      if (result.situationcategory != null)     {         if (situationcategory != null)         {             situationcategory.description = result.situationcategory.description;         }     }      await _context.savechangesasync(); } 

in screenshot, have highlighted data should updated:

enter image description here

please answer

an ef context knows nothing objects unless attach given object context, or, retrieved object context.

instead of marking entity modified:

_context.entry(result).state = entitystate.modified; 

you'll need call update(), which, begins tracking entity & marks modified, so, when call savechanges(), changes written db:

_context.update(result); 

ps. call savechanges() once, in case, @ end of method.


Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -