Entity Framewor Core Database.Setinitializer

Asked

Viewed 644 times

-1

The Entity Framework Core has the Database.SetInitializer()?

  • Try to explain your question better so someone can help you.

1 answer

2

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

The Database.SetInitializer() is a way to inform which strategy to use on startup in your database with Code First and may vary its configuration:

  • CreateDatabaseIfNotExists

    Create the database and its respective tables if they do not exist.

  • DropCreateDatabaseIfModelChanges

    Deletes the database if there have been changes in its entities and creates again the database and its respective tables.

  • DropCreateDatabaseAlways

    Deletes the database if it exists and re-creates every time your program was started, different from the previous one that only deletes the database if there is a change, in which case it deletes and re-creates its database.

  • Custom DB Initializer

    Create an initializer or strategy with your own settings.

If you still want to cancel the strategy just write in the parameter null

Database.SetInitializer<DbContext>(null);

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

  • Database.EnsureCreated();

    Create the database and its respective tables if they do not exist.

  • Database.EnsureDeleted();

    Excludes database if any.

Reference:

Browser other questions tagged

You are not signed in. Login or sign up in order to post.