Skip to content

Commit 2d45b93

Browse files
docs(grid): add new exception in the grid/editing/overview notes (#170)
* chore(grid): add new exception in the note which describing about the risks when you are using your EF models directly binded to the grid * chore(grid): simplify list nesting Co-authored-by: Marin Bratanov <m.bratanov@gmail.com>
1 parent 18ad21e commit 2d45b93

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

components/grid/editing/overview.md

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -244,12 +244,16 @@ There are a few considerations to keep in mind with the CUD operations of the gr
244244

245245
* 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.
246246

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:
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.
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 one of the following exceptions: `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...` or `This is a DynamicProxy2 error: The interceptor attempted to 'Proceed' for method 'Microsoft.EntityFrameworkCore.Infrastructure.ILazyLoader get_LazyLoader()' which has no target. When calling method without target there is no implementation to 'proceed' to and it is the responsibility of the interceptor to mimic the implementation (set return value, out arguments etc)`.
250+
251+
To fix it you can change the update using this approach:
252+
253+
1. Find the object in your database by Id (you can use `dbContext.Find()` or similar method depending on your infrastructure).
254+
1. Apply all the changes you need to it one by one - assign the values of all of its properties - `dbObject.Property1 = argsItem.Property1...`
255+
1. Call `dbContext.SaveChanges()`
248256

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()`
253257

254258

255259
## See Also

0 commit comments

Comments
 (0)