-1

This is my code, built on ASP.NET MVC and Entity Framework:

[HttpPost]
[Route("DeskBooking")]
public JsonResult DeskBooking(string dpStart, string dpEnd, int tmStart, int tmEnd)
{
    DateTime dpStartCon = DateTime.Parse(GetDateStart(dpStart));
    DateTime dpEndCon = DateTime.Parse(GetDateEnd(dpEnd));
        
    using (Models.nhsdmsEntities ctx = new Models.nhsdmsEntities())
    {
        List<Models.tblDeskBooking> tblDB = ctx.tblDeskBookings
                                               .Where(x => dpStartCon <= x.DateStart && 
                                                           x.DateEnd <= dpEndCon && 
                                                            tmStart >= x.TimeStart && 
                                                            tmEnd <= x.TimeEnd).ToList();
    return Json(new { data = tblDB }, JsonRequestBehavior.AllowGet);
}
}

The tblDB has 3 rows but still on the client side I get this error:

An unhandled exception was generated during the execution of the current web request
[ObjectDisposedException: The ObjectContext instance has been disposed and can no longer be used for operations that require a connection.]

Client-side code:

$(document).on("click", "#btnBookDeskEmp", function () {

    var dpStart = $("#dpStart").val();
    var dpEnd = $("#dpEnd").val();

    var tmStart = $("#tmStart").val();
    var tmEnd = $("#tmEnd").val();

    AjaxReq(function (data) {
    }, "DeskBooking", { dpStart: dpStart, dpEnd: dpEnd, tmStart: parseInt(tmStart), tmEnd: parseInt(tmEnd) });
})

function AjaxReq(callback, action, data) {
    $.ajax({
            url: "/Home/" + action,
            method: "POST",
            data: data,
        })
            .done(function (data) {
                callback(data);
            })
            .fail(function (e) {
                alert("error");
            })
            .always(function () {
                console.log("complete");
            });
}

        //------------------------------------------------------------------------------
    // <auto-generated>
    //     This code was generated from a template.
    //
    //     Manual changes to this file may cause unexpected behavior in your application.
    //     Manual changes to this file will be overwritten if the code is regenerated.
    // </auto-generated>
    //------------------------------------------------------------------------------

    namespace NHSDMS.Models
    {
        using System;
        using System.Data.Entity;
        using System.Data.Entity.Infrastructure;

        public partial class nhsdmsEntities : DbContext
        {
            public nhsdmsEntities()
                : base("name=nhsdmsEntities")
            {
            }

            protected override void OnModelCreating(DbModelBuilder modelBuilder)
            {
                throw new UnintentionalCodeFirstException();
            }

            public virtual DbSet<tblDesk> tblDesks { get; set; }
            public virtual DbSet<tblRoom> tblRooms { get; set; }
            public virtual DbSet<tblDeskBooking> tblDeskBookings { get; set; }
        }
    }
redoc01
  • 2,107
  • 5
  • 32
  • 64
  • 1
    That error message is from EntityFramework (the javascript code has has nothing to do with this error). Are you able to show us the code for: `Models.nhsdmsEntities`? – Hooman Bahreini Oct 25 '21 at 01:52
  • 1
    Your question might be a duplicate of [this one](https://stackoverflow.com/questions/18398356/solving-the-objectcontext-instance-has-been-disposed-and-can-no-longer-be-used) – Hooman Bahreini Oct 25 '21 at 01:54
  • @Hooman Bahreini,i have updated as you suggested – redoc01 Oct 25 '21 at 10:56
  • there is a missing `}` in your code... it is not clear where does the `using` block ends. – Hooman Bahreini Oct 25 '21 at 11:15
  • sorry my bad i must of missed out the curly bracket when pasting it inside SO – redoc01 Oct 25 '21 at 11:27
  • should i just delete the edmx file and create another one? – redoc01 Oct 25 '21 at 11:28
  • Is `return Json...` inside the using block? have you tried eager loading the entities as suggested in the question that I linked above? – Hooman Bahreini Oct 25 '21 at 11:50
  • yes the return json is in the using block and i tried the virtual stuff aswell in the link but no luck – redoc01 Oct 25 '21 at 11:51
  • not the `virtual`, I meant using `include` to eager load the related entities? – Hooman Bahreini Oct 25 '21 at 11:56
  • 1
    @HoomanBahreini, IT WORKED, using the link u suggested, i went back when u said.this was the line that solved the issue in the nhsdmsEntites class: this.Configuration.LazyLoadingEnabled = false; is this code ok to use i mean to set it to false? – redoc01 Oct 25 '21 at 11:56

1 Answers1

-1

In the edmx file, i had to delete everything from the navigation properties as this was messing up my namesspace. if you would like more info i can show sceenshots.

redoc01
  • 2,107
  • 5
  • 32
  • 64