This is a migrated thread and some comments may be shown as answers.

Entity framework and view models

2 Answers 155 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Emmanuel
Top achievements
Rank 1
Emmanuel asked on 21 Apr 2017, 06:43 PM

Hi,

I have the following code in my controller and in the mapping of my entity I navigate a foreign key. (Plate = anomalie.voiture.plaque)

That gives me the following exception:

An exception of type 'System.Data.Entity.Core.EntityCommandExecutionException' occurred in EntityFramework.dll but was not handled in user code

 

There is already an open DataReader associated with this Connection which must be closed first.

 

I tried to add an include() but doesn't solve the problem. Any ideas ?

public ActionResult Read([DataSourceRequest]DataSourceRequest request)
{
    using (var context = new carpackEntities())
    {
        IQueryable<anomalie> damages = context.anomalie;
        damages.Include(d => d.voiture);
        var result = Json(damages.ToDataSourceResult(request, damage =>
        new DamageViewModel()
        {
            Date = damage.dtajout,
            Description = damage.avarie,
            Driver = damage.chauffeur,
            Plate = damage.voiture.plaque,
            Service = damage.service,
            State = damage.etat.ToString()
        }));
        return result;
    }
}

2 Answers, 1 is accepted

Sort by
0
Viktor Tachev
Telerik team
answered on 25 Apr 2017, 08:54 AM
Hello Emmanuel,

The error you are seeing seems like an EntityFramework related error. Please examine the thread below that discusses similar error and what can cause it. 


With that said, try to map the fields first and then call ToDataSource result and see how the behavior changes. The code would look similar to the following.


public ActionResult Read([DataSourceRequest]DataSourceRequest request)
{
    using (var context = new carpackEntities())
    {
        IQueryable<anomalie> damages = context.anomalie;
        damages.Include(d => d.voiture);
         
        var result = damages.Select(damage=> new DamageViewModel()
            {
                Date = damage.dtajout,
                Description = damage.avarie,
                Driver = damage.chauffeur,
                Plate = damage.voiture.plaque,
                Service = damage.service,
                State = damage.etat.ToString()
            });
             
        return Json(result.ToDataSourceResult(request));
    }
}




Regards,
Viktor Tachev
Telerik by Progress
Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
0
Emmanuel
Top achievements
Rank 1
answered on 26 Apr 2017, 04:24 PM

Hi Viktor,

Thanks for your reply,

Indeed that's the solution I've found a bit after posting here (as always, everytime you ask a question on forum or SO you'll find the answer yourself 20 minutes later... :p)

That's working for me !

Tags
Grid
Asked by
Emmanuel
Top achievements
Rank 1
Answers by
Viktor Tachev
Telerik team
Emmanuel
Top achievements
Rank 1
Share this question
or