Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/321.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# 实体框架中具有虚拟属性的System.ObjectDisposedException_C#_Asp.net Mvc_Entity Framework - Fatal编程技术网

C# 实体框架中具有虚拟属性的System.ObjectDisposedException

C# 实体框架中具有虚拟属性的System.ObjectDisposedException,c#,asp.net-mvc,entity-framework,C#,Asp.net Mvc,Entity Framework,嗨,有人能帮我吗?当我试图在视图中显示有关Carmodel的数据时,我遇到了上述错误 [Key] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int CarId { get; set; } [Required] public string Registration { get; set; } [Required]

嗨,有人能帮我吗?当我试图在视图中显示有关Carmodel的数据时,我遇到了上述错误

        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int CarId { get; set; }

        [Required]
        public string Registration { get; set; }

        [Required]
        public virtual CarModels Model { get; set; }

        [Required]
        public string RegistrationYear { get; set; }

        [Required]
        public string ChassisNumber { get; set; }

        [Required]
        public int RegistrationId { get; set; }
这里是函数

public static List<Cars> GetRegistrationCars(int registration)
        {
            List<Cars> registrationCars = new List<Cars>();
            using (var db = new EventsContext())
            {
                registrationCars = db.Cars.Where(c => c.RegistrationId == registration).ToList();
            }

            return registrationCars.ToList();
        }
公共静态列表GetRegistrationCars(int注册)
{
列表登记车辆=新列表();
使用(var db=new EventsContext())
{
registrationCars=db.Cars.Where(c=>c.RegistrationId==registration.ToList();
}
返回注册车辆。ToList();
}

在对上下文调用
ToList()
之前,您正在处理该上下文。使用块将
ToList()移动到
内部

    public static List<Cars> GetRegistrationCars(int registration)
    {
        List<Cars> registrationCars = new List<Cars>();
        using (var db = new EventsContext())
        {
            registrationCars = db.Cars.Where(c => c.RegistrationId == registration).ToList();
            return registrationCars.ToList();
        }
        // Here is too late - using has Disposed() the EventsContext() already so ToList will throw an exception
     }
公共静态列表GetRegistrationCars(int注册)
{
列表登记车辆=新列表();
使用(var db=new EventsContext())
{
registrationCars=db.Cars.Where(c=>c.RegistrationId==registration.ToList();
返回注册车辆。ToList();
}
//现在已经太晚了-使用has Disposed()已处理EventsContext(),因此ToList将引发异常
}
编辑:


这是假设linq查询上的
ToList
调用实际上不在那里(第一轮我没有发现),但现在我希望它是一个输入错误:)

它试图在返回列表(并且
DbContext
被释放)后延迟加载
模型
属性。要么立即加载
模型
属性,要么禁用延迟加载/代理生成。

啊哈,最后终于解决了,谢谢你的建议

 public static List<Cars> GetRegistrationCars(int registration)
        {
            List<Cars> registrationCars = new List<Cars>();

            using (var db = new FerrariEventsContext())
            {
                registrationCars = db.Cars.Include(m=> m.Model).Where(c => c.RegistrationId == registration).ToList();
            }

            return registrationCars;
        }
公共静态列表GetRegistrationCars(int注册)
{
列表登记车辆=新列表();
使用(var db=new FerrariEventsContext())
{
registrationCars=db.Cars.Include(m=>m.Model).Where(c=>c.RegistrationId==registration.ToList();
}
归还登记车辆;
}

还有一个建议:
GetRegistrationCars()
应该是异步的-通过将
ToList()
更改为Waiting
ToListSync()

是的,之前没有注意到。。。通常在列表上调用
ToList()
是毫无意义的-被滚动条挫败了哈哈:)是的,对不起,不应该包括这一点,那是我抓紧吸管再次添加的,那是打字错误吗?Linq查询的ToList调用,或者它实际上在那里?如果没有它,你会得到你提到的错误。。。有了它,我不太确定!实际上,从您声明的
registrationCars
列表来看,它看起来无论如何都必须调用ToList,否则
IEnumerable
结果将不适合该类型-必须是其他类型-您从何处获得异常?抛出来自哪一行?要禁用延迟加载:yourContextName.Configuration.LazyLoadingEnabled=false;仅当方法也被声明为
async
,并且仅当应用程序通常使用
async/await
模型时。这与问题无关。