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
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...
Answer
#2: Post edited
- 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
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(); ```