This project is read-only.

ThemedDataGridFor with Self-Tracking Entity

Apr 19, 2011 at 2:39 PM


I'm learning .NET with a new project based in the NLayerApp (Microsoft - Spain) and your ToolKit (MVC3).
I am trying create my app based int the MVC3 themed exemple from the Download page.
I would like to use some of my domain Self-Tracking entities with ThemedDataGridFor, but when I do this I get a exception: "Object reference not set to an instance of an object".
I think the error is related with the Self-Traking implemention, because if I use the entity without Self-Tracking it done.
I would not like create a POCO entity for the MVC model to each my domain entities.
In the View I use:

            m => m.ModelList,
            GridFeatures.Display |
            GridFeatures.Edit | 
            GridFeatures.Delete |
            GridFeatures.Insert |
            GridFeatures.Paging |
            GridFeatures.UndoEdit |
            new Columns<Microsoft.Samples.NLayerApp.Domain.MainModule.Entities.Projeto>()
                .Add(m => m.proj_codigo, FieldFeatures.Sort)
                .Add(m => m.proj_descr, FieldFeatures.Sort),
             m => m.OrderBy,
             m => m.CurrPage,
             m => m.PrevPage,
             m => m.TotalPages)

And I get a exception in the "Views\Shared\Themes\Test\Datagrid.cshtml", line: "@DataGridHelpers.DataGridFor(options.HtmlHelper,..."
Could you help me with this problem?

Apr 20, 2011 at 12:18 AM

Unluckly, Self Tracking entities creates problems in the User interface(not only in the grid) for several reasons:

1) Due to lazy-loading if they are connected to other entities this other entities are NOT loaded before you put your object in the View. However, when the View try to access some property the original context might be already destroyed , thus causing null reference errors.

2) Self-Tracking entities automatically imports database constraints imposed on the properties, so if for instance you don't save the key in a hidden field when the model binder try to read back your model you might experience a validation error, because a required field has not been provided.

In your case probably the grid is trying to read some property containing other entitie of a 1 to N relation and it fails because of point 1).

The point is that the grid do changes-tracking to allow user undo changes in case of errors, and to make possible to understand what kind of operation need to be passed to the DB (insert, update, or delete) so it tries to save ALL ORIGINAL VALUES OF ALL PROPERTIES into an hidden filed. This way it is able to rebuild the Old state of each row. 

In the next version 1.0 I give the possibility to define exiplicitely the felds whose OLd value need to be saved. In case you use the themed datagrid it will assume they are the same of the columns you provided, so you don't need to provide any further information.

This should solve probably your problem, because in this case the Grid will not attempt to read connection properties but only the property provided in your column definition. However you might continue experiencing problems because of problem 2). In particular you will be forced to provide in the View a value for all properties defined as required in the database(not nullable).  

This problem is not specific of the grid...but is the reason why most of people prefer poco entities...;)

The new version 1.0 should be available in a couple of Days.

Apr 20, 2011 at 11:58 AM

Thank you!
I didn't know these problems with Self-tracking in the view. I will try a diferente approach.
Thank you for your framework! It will be helpfull for me!