C# - The ObjectContext instance has been disposed and can no longer be used for operations that require a connection

后端 未结 5 1863
时光说笑
时光说笑 2021-01-19 08:29

I\'m sorry for the repetition. I\'m heading with a same problem but still could not handle it. I\'m using Angularjs framework and Asp.net mvc.

My person class:

相关标签:
5条回答
  • 2021-01-19 08:36

    The problem is solved. I get help from my friend. Its about unclosed connection. Its my fault. I didn't mention it.

    In PersonIndexVM(), I created People = new List<Person>(); Person class is created by entity framework. It is related with database. When I create a model that is an object of PersonIndexVM() in GetPeople() method and return this model object as a json, model object try to reach User class informations after the connection closed. And I'm getting this error. To solve this problem:

    1. Closing the lazy loading to prevent reaching User information. dc.Configuration.LazyLoadingEnabled = false;

    2. Creating another class not related with database and return its object as Json.

    0 讨论(0)
  • 2021-01-19 08:43

    In my case, i was passsing all model 'Users' to a column, and it doesn't mapped correctly, so i just pass 'Users.Name' and it fixed it.!!

    var data = db.ApplicationTranceLogs
    .Include(q=>q.Users).Include(q => q.LookupItems)
    .Select(q => new { Id = q.Id, FormatDate = q.Date.ToString("yyyy/MM/dd"), Users = ***q.Users***, ProcessType = q.ProcessType, CoreProcessId = q.CoreProcessId, Data = q.Data })
    .ToList();
    

    --

    var data = db.ApplicationTranceLogs
    .Include(q=>q.Users).Include(q => q.LookupItems)
    .Select(q => new { Id = q.Id, FormatDate = q.Date.ToString("yyyy/MM/dd"), Users = ***q.Users.Name***, ProcessType = q.ProcessType, CoreProcessId = q.CoreProcessId, Data = q.Data })
    .ToList();
    
    0 讨论(0)
  • 2021-01-19 08:49

    It's look like the problem is when you using keyword Using.

    Look at this How to solve the error The ObjectContext

    0 讨论(0)
  • 2021-01-19 08:53

    In my case I had a bug in the front end, which was causing the same function in the backend to be triggered 3 times, causing a threading issue. Maybe look into this as a possibility too.

    0 讨论(0)
  • 2021-01-19 08:58

    You can strongly type your inclusion, which will give you hints as to whether or not your object structure is correctly related. This solves any "magic strings" issues, such as your table being named Users instead of User inside of your EF context, after including DbExtension.

    using System.Data.Entity.DbExtension;
    model.People = dc.People.Include(c => c.User).ToList();
    

    However, if you are using ObjectContext instead of DbContext, you may be stuck with magic strings. That said, EF will "pluralize" most names, so your table is most likely named "Users". Check to see if the table exists as dc.Users and, if it does, change your magic string to match.

    model.People = dc.People.Include("Users").ToList();
    
    0 讨论(0)
提交回复
热议问题