首页 > 解决方案 > 似乎无法使用 ASP.NET MVC 和实体框架返回我的模型

问题描述

这是我的代码,基于 ASP.NET MVC 和实体框架构建:

[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);
}
}

tblDB3 行但仍在客户端我收到此错误:

在执行当前 Web 请求期间生成了未处理的异常
[ObjectDisposedException:ObjectContext 实例已被释放,不能再用于需要连接的操作。]

客户端代码:

$(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; }
        }
    }

标签: jsonasp.net-mvcentity-framework

解决方案


在 edmx 文件中,我不得不从导航属性中删除所有内容,因为这弄乱了我的命名空间。如果您想了解更多信息,我可以显示截图。


推荐阅读