Place to put Database.SetInitializer

前端 未结 5 1448
萌比男神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 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());
    }
    

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

提交回复
热议问题