4

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:

public partial class Person
{
    public int PersonID { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Telephone { get; set; }
    public string Adress { get; set; }
    public int UserID { get; set; }

    public virtual User User { get; set; }
}

My User class:

public partial class User
{
    public User()
    {
        this.People = new HashSet<Person>();
    }

    public int UserID { get; set; }
    public string Username { get; set; }
    public string Password { get; set; }
    public string FullName { get; set; }
    public string Email { get; set; }
    public string Gender { get; set; }

    public virtual ICollection<Person> People { get; set; }
}

My js code:

 $http.get('/Data/GetPeople', { params: { UserID: "1" } }).success(function (data) {
        $scope.model = data;
    });

I'm trying to get records from my database:

 public ActionResult GetPeople(int UserID)
    {

        using (PersonEntities dc = new PersonEntities())
        {
            var model = new PersonIndexVM();
            model.People = dc.People.Where(b => b.UserID == UserID).ToList();
            //model.People = dc.People.Include("User").ToList();
            return Json(model, JsonRequestBehavior.AllowGet);
        }

    }

As I see with debugging, I'm getting the right objects from database in GetPeople method. But after that I'm getting this error:

'The ObjectContext instance has been disposed and can no longer be used for operations that require a connection.'

I tried to Eagerly load: model.People = dc.People.Include("User").Where(b => b.UserID == UserID).ToList(); Still getting the same error.

It would be a pleasure if you help me.

Community
  • 1
  • 1
bahardvan
  • 81
  • 1
  • 1
  • 6

5 Answers5

4

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.

bahardvan
  • 81
  • 1
  • 1
  • 6
2

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();
David L
  • 32,885
  • 8
  • 62
  • 93
  • In `c => c.User` part, im getting the **cannot convert lambda expression to type 'string' because it is not a delegate type** error. – bahardvan Jul 21 '15 at 14:36
  • Product version: 6.1.1 – bahardvan Jul 21 '15 at 14:45
  • @bahardvan Please see updates, particularly the `DbExtension` namespace inclusion. – David L Jul 21 '15 at 14:55
  • I corrected with `using System.Data.Entity`. But unfortunately the problem is still same. I tried all of them c => c.User, "Users" – bahardvan Jul 21 '15 at 15:05
  • @bahardvan then there has to be something else that is trying to include a child table. Is there any other code on this call? What line does it blow up on exactly? – David L Jul 21 '15 at 15:06
  • After it is exited from the ActionResult GetPeople scope, it blows up. Actually it worked fine before i added a userId to person table and set it as foreign key to user table. – bahardvan Jul 21 '15 at 15:20
  • What does the User class look like? Perhaps you're missing additional dependencies there. – David L Jul 21 '15 at 15:49
  • @bahardvan Your relationship looks circular. You have a user on a person, but a collection of people on your user. – David L Jul 21 '15 at 16:11
  • My problem is about circular relationship? Please help what to do. Im already appreciate you taking the time. – bahardvan Jul 21 '15 at 19:33
  • @bahardvan I'd try removing `public virtual ICollection People { get; set; }` from `User` – David L Jul 21 '15 at 19:34
  • Ok i would try it tomorrow morning at the office. – bahardvan Jul 21 '15 at 20:55
  • Thanks for everything. I guess the problem is solved. – bahardvan Jul 22 '15 at 10:41
  • Thank you, @bahardvan! I could not for the life of my figure out how to get my lambda to work on in the .Include. I just needed to add the using. Cheers! – Oyyou Jun 09 '17 at 11:48
0

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

Look at this How to solve the error The ObjectContext

Community
  • 1
  • 1
Lior Dadon
  • 233
  • 2
  • 10
0

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();
Gabriel Heming
  • 1,100
  • 10
  • 30
0

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.

chris c
  • 321
  • 2
  • 14