Custom Database Initializer in Entity Framework

In this tutorial, we will show how to build a Custom Database Initializer in Entity Framework. In our last tutorial Database initializer in Entity Framework, we discussed the built-in initializers. The Entity framework comes with three built-in Initializers. Those are CreateDatabaseIfNotExists, DropCreateDatabaseAlways and DropCreateDatabaseIfModelChanges. You can build your own custom initializer if any of those does not satisfy your needs.

Custom Database Initializer in Entity Framework

Custom Database initializers are used when if any of the inbuilt initializers does not satisfy your needs. This is useful when you want to have full control over the creation of the database.

You can create the custom Initialiser either by implementing the interface IDatabaseInitializer or by inheriting from one of the built-in initializers.

Using IDatabaseInitializer

You can create Custom Initializer by implementing the IDatabaseInitializer Interface as shown below

In the above Code, we have created Custom initializer EFInitializer. This class implements the IDatabaseInitializer interface.

The IDatabaseInitializer Interface has one method, which we must implement in the derived class. i.e. InitializeDatabase. The InitializeDatabase method is called when the context is created.

In the InitializeDatabase method, we check whether the database exists or is it compatible with our current model. If not, then it proceeds to delete and create the database.

The behaviour of the above class is same as DropCreateDatabaseIfModelChanges built in initializer

Once you define your custom database initializer class, you can register it in the same way as the predefined initializer class

Using Built In Initializer

You can also use the built-in initializers to create the custom database initializer.

The above code creates a custom database initializer EFInitialise by inheriting from the DropCreateDatabaseIfModelChanges initializer. The InitializeDatabase is already implemented by the DropCreateDatabaseIfModelChanges.You can also override the InitializeDatabase provided by the base initializer. This allows you to add any custom logic. The example code is shown below

Registering the initializer in Configuration file

There are two ways you can register your initializer in entity framework. One using the Database.SetInitializer method as shown below

The other method is to add it to the configuration file. This is shown below.

Remember that the Initializer settings in configuration file take precedence over the initializer set using the SetInitializer method.

The Database class

The initialization of the database is done by the database class. We looked at this class in our previous tutorial. This class provides many methods, which makes it easier to initialize the database. Some of the methods are listed below.

Create

Creates a new database based on the model.

CreateIfNotExists

Creates a new database only if the database does not exist.

Delete

Deletes the database if it exists, otherwise does nothing.

ExecuteSqlCommand

Executes the given SQL query against the database

BeginTransaction

Begins a transaction on the underlying data store connection

Exists

Checks whether or not the database exists on the server.

Initialize

Runs the registered IDatabaseInitializer on this context.

SetInitializer

Sets the database initializer to use for the given context type.

Summary

In this tutorial, we looked at how to create the custom database initializer in entity framework. In the next tutorial, we look at how to seed the database using the custom database initializer to Seed Database in Entity Framework

1 thought on “Custom Database Initializer in Entity Framework”

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top