locked
check to see if a context is already registered RRS feed

  • Question

  • User-1712241964 posted

     Would anyone have an idea of how I should be checking to see if a model is already registered?
    Here is what I initially wanted to put in my global.asax:

    MetaModel model1 = new MetaModel();
    model1.RegisterContext(
    typeof(gmsdb1DataContext), new ContextConfiguration() { ScaffoldAllTables = false });

    I can't do it in the global.asax/'Application_Start', so I am trying to do it in the next best place, and HttpModule INIT event.
    Problem.   My page runs fine the first time it is used, but the second time it is used it errors saying that the "Item has already been registered....", so I figure I need the httpmodule to do a check to see if the context is already registered.
    Here is what I have so far, but that is not successfully registering the context:

    public class AddDD : IHttpModule
    {
       public void Init(HttpApplication context)
        {
            try
            {
                MetaModel model1 = MetaModel.GetModel(typeof(gmsdb1DataContext));
            }
            catch (InvalidOperationException)
            {
                MetaModel model1 = new MetaModel();
    
                model1.RegisterContext(typeof(gmsdb1DataContext), new ContextConfiguration() { ScaffoldAllTables = false });
            }
        }
       public void Dispose(){}
    }


    Any thoughts, ideas, or suggestions would be appreciated.

    -Gary

    Friday, September 25, 2009 10:23 AM

Answers

  • User1186257875 posted

     Isnt your RegisterContext in the wrong place?  Surely you could do this:

  • public class AddDD : IHttpModule   
  • {   
  •    public void Init(HttpApplication context)   
  •     {   
  •         try  
  •         {   
  •             MetaModel model1 = MetaModel.GetModel(typeof(gmsdb1DataContext));   
  •             model1.RegisterContext(typeof(gmsdb1DataContext), new ContextConfiguration() { ScaffoldAllTables = false });  
  •         }   
  •         catch (InvalidOperationException)   
  •         {   
  •             //Suppress exception
  •   
  •              
  •         }   
  •     }   
  •    public void Dispose(){}   
  • }  

    Would be better to somehow check if its already registered rather than that though..

  • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, September 28, 2009 2:26 PM