Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Post History

60%
+1 −0
Q&A Datagrid in MVVM saves edits but not inserts

Entity Framework is able to save the changes by using a tracking mechanism (i.e. what is added, deleted, removed). I guess _repository.GetAllGoals() implementation is something like _context.Goals...

posted 1y ago by Alexei‭  ·  edited 1y ago by Alexei‭

Answer
#2: Post edited by user avatar Alexei‭ · 2022-10-23T08:36:11Z (over 1 year ago)
added a note related to not working directly with EF models
  • Entity Framework is able to save the changes by using a tracking mechanism (i.e. what is added, deleted, removed).
  • I guess `_repository.GetAllGoals()` implementation is something like `_context.Goals` or `_context.Set<Goal>()`, so when you are constructing the `ObservableCollection` you actually get a list of EF models references. Any update (change in property value or any change in the child properties) of a list item is a change in the EF model and SaveChanges will know about it through the tracking mechanism.
  • However, when adding or removing items from your `ObservableCollection`, these changes will be done outside of the EF tracking mechanism. **EF can track such changes only when done via the `DbSet`**:
  • - `_context.Goals.Add(...)` or `_context.Set<Goal>.Add(...)`
  • - `_context.Goals.Remove(...)` or `_context.Set<Goal>.Remove(...)`
  • One way to do this is to identify added or removed items before `SaveChanges` and explicitly add them to the set. Something along the lines:
  • ```c#
  • // I assume that goal has some kind of auto-generated Id. If this is not the case (e.g. working with GUIDs), another mechanism should be used to identify the brand-new goals
  • var toAdd = _goals.Where(g => g.Id == 0);
  • _context.Goals.AddRange(toAdd);
  • _context.SaveChanges();
  • ```
  • Entity Framework is able to save the changes by using a tracking mechanism (i.e. what is added, deleted, removed).
  • I guess `_repository.GetAllGoals()` implementation is something like `_context.Goals` or `_context.Set<Goal>()`, so when you are constructing the `ObservableCollection` you actually get a list of EF models references. Any update (change in property value or any change in the child properties) of a list item is a change in the EF model and SaveChanges will know about it through the tracking mechanism.
  • However, when adding or removing items from your `ObservableCollection`, these changes will be done outside of the EF tracking mechanism. **EF can track such changes only when done via the `DbSet`**:
  • - `_context.Goals.Add(...)` or `_context.Set<Goal>.Add(...)`
  • - `_context.Goals.Remove(...)` or `_context.Set<Goal>.Remove(...)`
  • One way to do this is to identify added or removed items before `SaveChanges` and explicitly add them to the set. Something along the lines:
  • ```c#
  • // I assume that goal has some kind of auto-generated Id. If this is not the case (e.g. working with GUIDs), another mechanism should be used to identify the brand-new goals
  • var toAdd = _goals.Where(g => g.Id == 0);
  • _context.Goals.AddRange(toAdd);
  • _context.SaveChanges();
  • ```
  • **Note**: when working on larger applications, there is a clear separation between the UI and the data fetch/persistence. This can be done by avoiding working directly with EF models in the UI, but instead working with view models which are very similar to EF models, but include only the required properties (maybe not everything should be visible) and may define new properties.
  • The fetch and persistence flow would be like the following:
  • - **fetch** - get goals list (EF models) -> map them to a list of goal view models -> `ObservableCollection` is built based on that list
  • - **persistence** - the VM list is merged into the DbSet (i.e. add what's missing, update existing entities, remove what is extra).
#1: Initial revision by user avatar Alexei‭ · 2022-10-23T08:29:32Z (over 1 year ago)
Entity Framework is able to save the changes by using a tracking mechanism (i.e. what is added, deleted, removed). 

I guess `_repository.GetAllGoals()` implementation is something like `_context.Goals` or `_context.Set<Goal>()`, so when you are constructing the `ObservableCollection` you actually get a list of EF models references. Any update (change in property value or any change in the child properties) of a list item is a change in the EF model and SaveChanges will know about it through the tracking mechanism. 

However, when adding or removing items from your `ObservableCollection`, these changes will be done outside of the EF tracking mechanism. **EF can track such changes only when done via the `DbSet`**:

- `_context.Goals.Add(...)` or `_context.Set<Goal>.Add(...)`
- `_context.Goals.Remove(...)` or `_context.Set<Goal>.Remove(...)`

One way to do this is to identify added or removed items before `SaveChanges` and explicitly add them to the set. Something along the lines:

```c#
// I assume that goal has some kind of auto-generated Id. If this is not the case (e.g. working with GUIDs), another mechanism should be used to identify the brand-new goals
var toAdd = _goals.Where(g => g.Id == 0);
_context.Goals.AddRange(toAdd);
_context.SaveChanges();
```