c# - How to fix the error "The ObjectContext instance has been disposed" when we already included the entity? -


the answer looks obvious, but, facing different. have 3 tables.. clients related users related messages. idea can have message sent or nobody, but, have destination;

[message]  * useridfrom (int) nullable;   * useridto (int) not nullable; 

both fields related table

[users]  * userid (int);  * clientid (int) not null; 

that related table

[clients]  * clientid (int);  * cliname varchar; 

my edmx mapped tables following:

[message]  * useridfrom (int)    * useridto (int)   + _navigation properties_ +   * userfrom (0..1) related [users] (on navigation properties)   * userto   (1..*) related [users] (on navigation properties) 

and users mapped as:

[users]  * userid (int)  * clientid (int)  + _navigation properties_ +   * clients related [clients]  * messagefrom (0..1) related [message] (on navigation properties)   * messageto   (1..*) related [message] (on navigation properties) 

and have clients mapped:

[clients]  * clientid (int)  * clientname (string)  + _navigation properties_ +   * users related [user] 

the problem:

in controller, have actionresult return list of messages (logged in) received (or nobody). have following code:

return view(db.users.where(u => u.usuemail.trim() == user.identity.name.trim()).firstordefault().messageto); 

in view, try show text like: daniel(from) sent message stackoverflow(to)

for that, use following code (to show list of messages):

@foreach (messages message in model) {    <p><strong>@message.userfrom.clients.clientname</p> } 

the problem occours when try informations clients inside userfrom, however, can informations userto;

so, tried use include function, trying information sent (messagefrom). example:

using (entities db = new entities()){       return view(db.users.include("messagefrom").where(u => u.usuemail.trim() == user.identity.name.trim()).firstordefault().messageto); } 

or

using (entities db = new entities()){       return view(db.users.include("messagefrom.clients").where(u => u.usuemail.trim() == user.identity.name.trim()).firstordefault().messageto); } 

the interesting thing can information addressee, can't information sender.

i know how use include function (because using in situation), but, time don't know need do.


Comments