Paweł Staniec
Paweł Staniec

Reputation: 3181

Place to put Database.SetInitializer

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 ASP.NET MVC. Service project is place where I have my database context and models defined.

My Goal is to have minimum or possibly no references to EF specific code in my Web project. I want it to be independent so when I switch the dlls with service backend ( from let say SQL to XML or MySQL ) I shouldn't make multiple modifications in my MVC project.

This is how it looks :

Project layout

My Questions are: - so far I've found no example of using Database.SetInitializer in other place than Global.asax. I'd like to put database re-creation if model changed in my factory-like DatabaseContextProvider class or in service class that pulls out data from context and provides it to the UI with DTOs. Are there any cons of that location ? - I would like to have the context's connectionString to be configurable with Properties/Settings.settings file - is that reasonable ?

Upvotes: 36

Views: 47732

Answers (5)

user9542003
user9542003

Reputation: 49

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);

Upvotes: 0

Carsten F&#252;hrmann
Carsten F&#252;hrmann

Reputation: 3490

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.

Upvotes: 3

Ricardo Huertas
Ricardo Huertas

Reputation: 1306

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>());

Upvotes: 9

RonyK
RonyK

Reputation: 2674

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.

Upvotes: 38

Eranga
Eranga

Reputation: 32447

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.

Upvotes: 15

Related Questions