Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

purpose of Database.SetInitializer<ArContext>(null) inside of repository?

I am using entity framework and in my context inheriting from DbContext.

public class MyContext : DbContext, IMyContext
{
    static MyContext()
    {
        Database.SetInitializer<MyContext>(null);
    }
    //other stuff
}

What is the purpose of this line?

Database.SetInitializer<ArContext>(null)
like image 1000
Alex Gordon Avatar asked Sep 26 '16 19:09

Alex Gordon


People also ask

What is database SetInitializer?

The database initializer is called when a the given DbContext type is used to access a database for the first time. The default strategy for Code First contexts is an instance of CreateDatabaseIfNotExists<TContext>. C# Copy.

Where is database SetInitializer?

The Database. SetInitializer() method should be kept before the Context object is initialized. In ASP.NET web application, the best place will be in Application_Start() event of Global. asax file.

How will you specify the database initializer in configuration file?

A database initializer must implement the IDatabaseInitializer interface where the TContext generic type specifies the context type with which the initializer will be used. This means that if your app or tests have multiple different contexts then you can register an initializer for each of these context types.


2 Answers

You can turn off the DB initializer of your application. On the production environment where you don't want to lose the existing data.In such scenario you can turn off the initializer, as shown below.

 public MyContext()
    {
        Database.SetInitializer<MyContext>(null);//Disable initializer
    }

There are four different database initialization strategies:

  1. CreateDatabaseIfNotExists: This is default initializer. As the name suggests, it will create the database if none exists as per the configuration. However, if you change the model class and then run the application with this initializer, then it will throw an exception.
  2. DropCreateDatabaseIfModelChanges: This initializer drops an existing database and creates a new database, if your model classes (entity classes) have been changed. So you don't have to worry about maintaining your database schema, when your model classes change.
  3. DropCreateDatabaseAlways: As the name suggests, this initializer drops an existing database every time you run the application, irrespective of whether your model classes have changed or not. This will be useful, when you want fresh database, every time you run the application, like while you are developing the application.
  4. Custom DB Initializer: You can also create your own custom initializer, if any of the above doesn't satisfy your requirements or you want to do some other process that initializes the database using the above initializer.

Reference : Database Initialization Strategies

like image 93
Sampath Avatar answered Sep 21 '22 12:09

Sampath


The default database initializer in Entity Framework Code First is CreateDatabaseIfNotExists. As its name indicates, if the database does not exist it'll create it.

This behavior is good during development but when you go to production maybe you won't want to auto create your database.

If you want to disable the initializers you use the line you showed, so now you have full control over how the database will be created and evolve in time.

Other initializers:

DropCreateDatabaseIfModelChanges.

DropCreateDatabaseAlways

Custom DB Initializer

Check this to know more.

like image 31
Karel Tamayo Avatar answered Sep 21 '22 12:09

Karel Tamayo