It seems a lot harder than it should be.
I am writing an event logging site, using MVC3, SQL Compact Edition and Entity Frameworks Code first, and also using the NuGet package to develop Mvc Steven Sanders forests.
Since the list of events is unlikely to change much, I cache it in the global list in the Application_Start method:
var repo = new RaceEventRepository(); EventRaces = repo.All.Where(r => r.RaceName.Contains(eventName)).Select(r => r).ToList();
where RaceEventRepository is the repository class built by MvcScaffolding and does
EventContext context = new EventContext();
which is then used through the repository and (I guess) is deleted when the repository is deleted. and EventRaces is a globally accessible list.
My problem is that when I create a registrar record with a foreign key back to the RaceEvent, which is stored in EventRaces, I get the error message "An entity object cannot reference multiple instances of IEntityChangeTracker".
According to several blog posts and SO responses, I need to separate the cached objects from the context, as in Listing 1 of this post .
My problem is that using ObjectBrowser I cannot find anything using the Detach method. the context in the Repository does not have this. Individual DbSets in the context do not have one (although they have an Attach () method). System.Data.Object.ObjectSet has one, but I cannot find a mapping between DbSet and ObjectSet.
Clearly I'm missing something. Can someone point me in the right direction?
source share