row selection does not work on ef datamodel with navigation properties.

2 posts, 1 answers
  1. Liza
    Liza avatar
    3 posts
    Member since:
    Oct 2012

    Posted 29 Oct 2012 Link to this post

    When i set the property .Selectable() on a grid which is bound to a datamodel that has a parent model i get an error. 
    It looks like its trying to get the parent datamodel but it cant because the object context is closed.

     here is my code 
    @using Kendo.Mvc.UI
         var addressList = Model.DataList as List<Intelli.Data.Parties.EFDAL.Entity.GeoAddress>;
        .Columns(columns =>
                    columns.Bound(p => p.ID).Title("ID").Hidden(true);
                    columns.Bound(p => p.Address).Title("Address").Width(160);
                    columns.Bound(p => p.Region).Title("Region").Width(80);
                    columns.Bound(p => p.Zipcode).Title("Zipcode").Width(80);
                    columns.Command(commands => commands
                                                    .HtmlAttributes(new { style = "text-align: center" }));
                    columns.Command(commands => commands
                                                    .HtmlAttributes(new { style = "text-align: center" }));               
        .Scrollable(scr => scr.Enabled(true).Height(300))
        .Sortable(builder => builder.Enabled(true))
        .Resizable(resizing => resizing.Columns(true))
                        .DataSource(dataSource => dataSource

    GeoAddress datamodel has a parent model 'Person' in the ef model.

    the error i get is : 
     'The ObjectContext instance has been disposed and can no longer be used for operations that require a connection.'

    If i comment out these properties : 

      .DataSource(dataSource => dataSource

    Then it works.
    It looks like that setting selectable tries to call all model properties (and if one of those is lazy loading data from db error occurs). 
    I also have no problem on other models that do not have parent associations with other data models.

  2. Answer
    Vesselin Obreshkov
    Vesselin Obreshkov avatar
    81 posts
    Member since:
    Jan 2010

    Posted 29 Oct 2012 Link to this post

    You generally shouldn't use your EF models directly with your UI but rather use flattened ViewModels. Then you won't run into this problem. Look at AutoMapper - we use it a lot and it saves us a ton of time and coding. Keep in mind though if/when you start using their IQueryable.ToDataSourceResult() extension, you have to make sure your property names are the same between your EF and View models.
Back to Top