似乎无法使用 ASP.NET MVC 和实体框架返回我的模型

Posted

技术标签:

【中文标题】似乎无法使用 ASP.NET MVC 和实体框架返回我的模型【英文标题】:Can't seem to return my model using ASP.NET MVC and Entity Framework 【发布时间】:2021-12-10 12:51:31 【问题描述】:

这是我的代码,基于 ASP.NET MVC 和 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);


tblDB 有 3 行但仍在客户端我收到此错误:

在执行当前网络请求期间产生了一个未处理的异常 [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; 
        
    

【问题讨论】:

该错误消息来自EntityFramework(javascript代码与此错误无关)。您能否向我们展示以下代码:Models.nhsdmsEntities 您的问题可能与this one重复 @Hooman Bahreini,我按照你的建议更新了 您的代码中缺少...尚不清楚using 块在哪里结束。 @HoomanBahreini,它工作了,使用你建议的链接,当你说时我回去了。这是解决 nhsdmsEntites 类中问题的行:this.Configuration.LazyLoadingEnabled = false;这段代码可以使用吗?我的意思是将其设置为 false? 【参考方案1】:

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

【讨论】:

以上是关于似乎无法使用 ASP.NET MVC 和实体框架返回我的模型的主要内容,如果未能解决你的问题,请参考以下文章

实体框架探查器 - 带有 EF 6 的 ASP.NET MVC4 - 无法确定提供程序名称

带有实体框架和 SQL Server 数据库的 ASP.NET MVC - 图像未显示在视图中......错误显示“无法将“字节”转换为“字符串”

与实体框架 ASP.net MVC 中多个表的可选关系

ASP.NET C# MVC 实体框架

如何在我的实体框架和 ASP.NET MVC 项目中重构 connectionString?

ASP.NET MVC3 和实体框架代码优先架构