17

I can't find examples on how to use EF Database Initializers with EF Core nor can I find a list of EF Database Initializers shipping with EF Core.

Has the concept of Database Initializers become obsolete? What's the current approach of initializing a database ... or not initializing an existing database?

AxD
  • 2,714
  • 3
  • 31
  • 53
  • Have you ckecked this https://stackoverflow.com/questions/40549735/entityframeworkcore-how-to-initialize-a-database-and-seed-it-the-first-time-use? – H. Herzl Nov 02 '17 at 07:13
  • 1
    Thank you for providing me with that link, but it's insufficient for my needs. I'm looking for a comprehensive, thorough documentation on this feature, rather than just a comment from an issue. The comment particularly doesn't yield information on the different options to _not_ touch the database structure on application start or on how to create custom initializers. – AxD Nov 02 '17 at 11:54
  • What exactly are you trying to accomplish? EF Core won't try and create database tables or apply migrations unless you call the API to do so. – bricelam Nov 02 '17 at 17:43
  • You can add an extension method for DbContext and seed on start API, why you don't like that solution? – H. Herzl Nov 03 '17 at 03:14
  • 2
    The issue is that I'm currently not sure how many rights I will get on the database. I sure won't want to update the database in production. So I'm looking for the Core way of defining (custom) database initializers. – AxD Nov 03 '17 at 14:17
  • @bricelam EF6 did exactly that. – John Aug 10 '18 at 15:56
  • ...and EF Core doesn't. – bricelam Aug 21 '18 at 22:42

1 Answers1

4

In .Net core you can use functions of Database property form DbContext instance. It allows you to create database (EnsureCreated), destroy database (EnsureDeleted) and migrate to last migration (Migrate). Required data should be placed in migrations. Look at last section of https://learn.microsoft.com/ef/core/managing-schemas/migrations/ for more details.

P.Mar
  • 57
  • 3