Place to put Database.SetInitializer

前端 未结 5 1422
萌比男神i
萌比男神i 2020-11-30 07:10

I\'m working on a project that at can end up with multiple UI versions / variants, but so far I\'ve got two subprojects in my solution Web - containing Web interface with A

相关标签:
5条回答
  • 2020-11-30 07:59

    I put it on the DbContext constructor and works for me.

    public myDbContext() : base(connectionToDatabase) {
            Database.SetInitializer<myDbContext>(null);
        }
    

    The solution above will work, but it is not as efficient as the following code:

      protected void Application_Start()
        {
            Database.SetInitializer<myDbContext>(null);
        }
    

    In my case I don't have a reference of my DAL on the UI and for that reason what I did is, I create a EntityFramework config and register my setting using using reflection.

     protected void Application_Start()
        {           
            EntityFrameworkConfig.RegisterSettings();
        }
    
    
     public static class EntityFrameworkConfig
    {
        public static void RegisterSettings()
        {
            // Use the file name to load the assembly into the current
            // application domain.
            Assembly a = Assembly.Load("MyAssembly");
            // Get the type to use.
            Type myType = a.GetType("MyType");
            // Get the method to call.
            MethodInfo myMethod = myType.GetMethod("MySettingsMethod");
            // Create an instance.
            object obj = Activator.CreateInstance(MyType);
            // Execute the method.
            myMethod.Invoke(obj, null);
        }
    }
    
      public void Configurations()
        {
          //Other settings
            Database.SetInitializer<myDbContext>(null);
        }
    

    Updated

    With Entity Framework 6, now you can use the NullDatabaseInitializer

    Database.SetInitializer(new NullDatabaseInitializer<MyDbContext>());
    
    0 讨论(0)
  • 2020-11-30 08:05

    To avoid the coupling, I would prefer not to set the initializer outside the Assembly that contains the DataContext. So, I added a static constructor for the DataContext. This way every project referencing this Assembly will enjoy the initializer without explicitly setting it, and the initializer is set only once per process.

    static MyDataContext()
    {
       Database.SetInitializer(new MigrateDatabaseToLatestVersion<MyDataContext, Configuration>());
    }
    

    The connection string will of course be taken from the application configuration file.

    0 讨论(0)
  • 2020-11-30 08:05

    Click Global.asax page and you find a Application_Start() method.Inside this method past this following code.For support this code use namespace using System.Data.Entity;

    Database.SetInitializer<namespace.modelclass>(null);
    
    0 讨论(0)
  • 2020-11-30 08:09

    You would need a mechanism to call the Database.SetInitializer method before the very first usage of the DbContext. That is why its usually called in the Global.asax file.

    You can create a class with an initialization method in your tm.Service project and call it in the Application_Start method and put the Database.SetInitializer in that initialization method.

    Its OK to supply the connection string from a setting file.

    0 讨论(0)
  • 2020-11-30 08:11

    Microsoft made it possible, for EF6 onwards, to configure one initializer per database context in the config file of the application. See the last section on this Microsoft page: https://msdn.microsoft.com/en-us/data/jj556606.aspx

    This, like the "Global.asax" approach, has the advantage that e.g. unit test projects can use a different initializer for the same database context.

    0 讨论(0)
提交回复
热议问题