Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/267.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 实体框架实体中延迟加载错误_C#_Asp.net Mvc_Linq_Entity Framework_Linq To Entities - Fatal编程技术网

C# 实体框架实体中延迟加载错误

C# 实体框架实体中延迟加载错误,c#,asp.net-mvc,linq,entity-framework,linq-to-entities,C#,Asp.net Mvc,Linq,Entity Framework,Linq To Entities,我的网页出现错误: {“ObjectContext实例已被释放,不能再用于需要连接的操作。”} 我的观点是: @model Service_Monitor_Web_Interface.Models.DashBoardViewModel ... => @Html.DisplayFor(modelItem => item.Service.Name) 以下是我的ViewModel类: public class DashBoardViewModel { publi

我的网页出现错误:

{“ObjectContext实例已被释放,不能再用于需要连接的操作。”}

我的观点是:

@model Service_Monitor_Web_Interface.Models.DashBoardViewModel
...    
=> @Html.DisplayFor(modelItem => item.Service.Name)
以下是我的ViewModel类:

    public class DashBoardViewModel
{

    public int NumberOfUsers { get; set; }

    public virtual IEnumerable<ApplicationUser> recentUsers { get; set; }

    public virtual IEnumerable<UserActivityLog> logs { get; set; }

    public virtual IList<Service_Monitor_Domain.Entities.ServiceStatusHistory> serviceStatus { get; set; }

}
下面是我访问数据库的函数:

  public List<ServiceStatusHistory> GetAllEnabledLatestStatusLog()
    {
        List<ServiceStatusHistory> list = null;
        try
        {
            using (var db = new EFDbContext())
            {
                //var results = db.ServiceStatusHistory.Where(h => h.Service.Enabled)
                //                  .OrderByDescending(h => h.Id)
                //                  .GroupBy(h => h.Service.Id)
                //                  .Select(grp => grp.FirstOrDefault());

                var results = db.ServiceStatusHistory
                            .Where(x => x.Service.Enabled)
                            .GroupBy(x => x.Service)
                             .Select(x => x.OrderByDescending(y => y.time).FirstOrDefault());

                list = results.ToList();
            }
            return list;
        }
        catch
        {
            //Error
        }
        return list;
    }
我认为这与延迟加载有关。但是我以前在查询同一个表时没有遇到过这个问题?不过,这只是一个简单的select查询。

考虑在GetAllEnabledLatestStatusLog()中“急切”加载服务类。确保包括使用System.Data.Entity
使用System.Data.Entity
var结果=db.ServiceStatusHistory
.包括(“服务”)
.Where(x=>x.Service.Enabled)
.GroupBy(x=>x.Service)
.Select(x=>x.OrderByDescending(y=>y.time).FirstOrDefault());

请参见

我想说,这里的问题是,在您的查询中,您没有显式地加载相关实体(在本例中,
ServiceStatusHistory.Service
),默认情况下,延迟加载是打开的

因此,稍后当您参考
item.Service.Name
时,底层的EF类意识到它需要加载这些相关的实体——除了关联的
ObjectContext
(在本例中,
EFDbContext
)早已被释放

因此,解决此问题的最简单方法可能是将查询修改为相关的
服务
实体

 var results = db.ServiceStatusHistory
                    .Include("Service")
                    .Where(x => x.Service.Enabled)
                    .GroupBy(x => x.Service)

                    .Select(x => x.OrderByDescending(y => y.time).FirstOrDefault());
或者,您可以关闭该查询:

using (var db = new EFDbContext())
{
   db.Configuration.LazyLoadingEnabled=false;
   //use original query here
}

我认为这是“服务”,而不是“服务”。使用严格键入的include版本来消除此类键入错误:。include(x=>x.Service)。@JimSTAT请查看我对其他答案的评论,因为它也与您的答案相关。“包含”在visual studio中以红色下划线显示。@Zapnologica请确保使用System.Data包含。Entity@JimSTAT,它现在在执行结果后抛出一个错误:“无法强制转换类型”System.Linq.IQueryable
1[[System.Linq.IGrouping
2[[Service_Monitor_Domain.Entities.Service,Service Monitor Domain,Version=1.0.0.0,Culture=neutral,PublicKeyToken=null],[Service_Monitor_Domain.Entities.ServiceStatusHistory,Service Monitor Domain,Version=1.0.0.0,Culture=neutral,PublicKeyToken=B77A561934E089]]'to type`to continueI修复了它:下面是有效的查询:`var result=db.ServiceStatusHistory.Include(“服务”)。其中(x=>x.Service.Enabled)。GroupBy(x=>x.Service).Select(x=>x.OrderByDescending(y=>y.time).FirstOrDefault());`I更改了查询,在Include:
System.Linq.IQueryable'上出现错误。它不包含'Include'的定义,并且没有扩展方法'Include'接受'System.Linq.IQueryable'类型的第一个参数。
`db.ContextOptions.LazyLoadingEnabled=false;`也不起作用。它说
EFDbContext不包含contextOptions的定义
@Zapnologica-yikes,我的错;请参阅更新的答案,它现在应该可以工作了。如果没有,请忽略第二个选项,因为第一个选项更安全(因为它不会急于加载任何内容,除非您告诉它)谢谢,关于关闭延迟加载。我的结果相当奇怪。如果我关闭延迟加载,我的查询将返回一个包含4项的列表。服务将对第一项有效,在这之后,即第2-4项为空?好的,在这种情况下,可能最好使用
include
方法,就像我前面提到的那样无论如何,它可能更好,因为它可以让你精确地控制想要加载的内容。
 var results = db.ServiceStatusHistory
                    .Include("Service")
                    .Where(x => x.Service.Enabled)
                    .GroupBy(x => x.Service)

                    .Select(x => x.OrderByDescending(y => y.time).FirstOrDefault());
using (var db = new EFDbContext())
{
   db.Configuration.LazyLoadingEnabled=false;
   //use original query here
}