Entity framework and view models

3 posts, 0 answers
  1. Emmanuel
    Emmanuel avatar
    7 posts
    Member since:
    Feb 2017

    Posted 21 Apr Link to this post

    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. Viktor Tachev
    Admin
    Viktor Tachev avatar
    1727 posts

    Posted 25 Apr Link to this post

    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.
  3. Emmanuel
    Emmanuel avatar
    7 posts
    Member since:
    Feb 2017

    Posted 26 Apr in reply to Viktor Tachev Link to this post

    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 !

Back to Top