Skip to content

Commit 60e30e1

Browse files
chore(grid): add additional information in grid editing notes about working directly with ef models (#169)
* chore(grid): add additional information in grid editing notes about working directly with ef models * chore(grid): minor imprvements Co-authored-by: Marin Bratanov <m.bratanov@gmail.com>
1 parent 5379bcd commit 60e30e1

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

components/grid/editing/overview.md

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@ CRUD operations with the Grid for Blazor are done through the dedicated CRUD eve
1414

1515
Sections in this article:
1616

17-
* [Basics](#basics)
18-
* [Example](#example)
19-
* [Notes](#notes)
17+
- [Basics](#basics)
18+
- [Example](#example)
19+
- [Notes](#notes)
20+
2021

2122
## Basics
2223

@@ -243,6 +244,13 @@ There are a few considerations to keep in mind with the CUD operations of the gr
243244

244245
* If you want to pre-populate values to the user, see the [Setting default values in new row]({%slug grid-kb-default-value-for-new-row%}) KnowledgeBase article.
245246

247+
* While editing, the Grid creates a **copy of your original object** which has a **different reference**. You receive that copy in the `OnEdit` and `OnUpdate` event handlers. This results in the following specifics:
248+
249+
* When you are using your Entity Framework models directly in the Grid (especially in a server-side Blazor scenario) and you use the `Item` property of `GridCommandEventArgs` directly in your DataBase update method, you can get the following exception: `The instance of entity type 'YourModel' cannot be tracked because another instance with the same key value for {'Id'} is already being tracked. When attaching existing entities, ensure that only one entity instance with a given key value is attached...` To fix it you can change the update using this approach:
250+
1. Find the object in your database by Id (you can use `dbContext.Find()` or similar method depending on your infrastructure).
251+
1. Apply all the changes you need to it one by one - assign the values of all of its properties - `dbObject.Property1 = argsItem.Property1...`
252+
1. Call `dbContext.SaveChanges()`
253+
246254

247255
## See Also
248256

0 commit comments

Comments
 (0)