views:

22

answers:

2

If I've got a setup where my master page has a member which is an EF generated object, and I want to use this member then within the page itself (but have need for it within the master page as well).

Currently I've got a using loop within the Master Page Page_Init event to establish the member. However if then I try to get any properties of this within the Page I get an error about "The ObjectContext instance has been disposed...", which is fair enough (It is within it's own using loop, however that's a different OC instance, so it's throwing this error.)

What's the best/preferred way around this situation? Is it to open the Object Context within the MasterPage pre_init event, and then dispose of it during the Page_Dispose event of the Master Page, or use a second OC Instance in the Page, and just pull a local version of the EF object by checking it's ID against the Master Page's object ID?

Thanks, Psy

+1  A: 

Can't you create a POCO when you load the master page and make it a protected property which your derived pages can get access to. Results in 1 trip to the database.

James
Agreed. +1. Better yet chuck it in a repository.
RPM1984
A: 

It's best practice not to use the Object Context in your UI layer to begin with. You should a business/data access layer to broker the communication with the database.

e36M3