Andrea Azzola

Tips and Techniques for Lifestyle Design

Check the MetaModel Existence while Running a Dynamic Data Application

Posted on [Permalink]

In the web application that I'm developing using Dynamic Data, I set the DataContext at runtime, the application might bind different contexts, depending on many variables. I usually test it on two different browser, and, because of the session, I need to pass the same procedure twice. The second time, the DataContext it's already set so I don't need to register the context anymore. The simple condition I use:

if (System.Web.DynamicData.MetaModel.Default == null)
    // Register context and routes

Othwerwise I would get this exception:

[ArgumentException: L'elemento è già stato aggiunto. Chiave nel dizionario: 'MyAppDataContext'. Chiave aggiunta: 'MyAppDataContext']

The exception meaning: The item is already present, Key in dictionary 'MyAppDataContext', Key added 'MyAppDataContext'.