Entity Framewor Core Database.SetInitializer [closed]

-1

Does Entity Framework Core have Database.SetInitializer() ?

    
asked by anonymous 24.05.2018 / 14:13

1 answer

1

It does not have this method that was previously used in the Entity Framework 6.x version, with Entity Framework Core being used for its context DbContextOptions ( DbContextOptions<T> ).

Database.SetInitializer() is a way to tell you which strategy to use at startup in your database with Code First and can vary your configuration:

  • CreateDatabaseIfNotExists

    Create the database and its tables if it does not exist.

  • DropCreateDatabaseIfModelChanges

    Deletes the database if there were changes to its entities and re-creates the database and its tables.

  • DropCreateDatabaseAlways

    Delete the database if it exists and re-create every time your program started, other than the previous one, which only excludes the database if it changes, in which case it deletes and rebuilds its database.

  • Custom DB Initializer

    Create an initializer or strategy with your own settings.

If you still want to undo the strategy, just write the parameter null

Database.SetInitializer<DbContext>(null);

In Entity Framework Core it is simpler to call this initialization process with methods:

  • Database.EnsureCreated();

    Create the database and its tables if it does not exist.

  • Database.EnsureDeleted();

    Deletes the database if it exists.

Reference:

24.05.2018 / 14:50