Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat is the correct use of IDatabaseInitializer in EF?
    text
    copied!<p>I have a custom DatabaseInitialiser which is below</p> <pre><code>/// &lt;summary&gt; /// Implements the IDatabaseInitializer to provide a custom database initialisation for the context. /// &lt;/summary&gt; /// &lt;typeparam name="TContext"&gt;TContext is the DbContext&lt;/typeparam&gt; public class ParikshaDataBaseInitializer&lt;TContext&gt; : IDatabaseInitializer&lt;TContext&gt; where TContext : DbContext { /// &lt;summary&gt; /// The method to Initialise the database. /// Takes care of the database cannot be dropped since it is in use problem while dropping and recreating the database. /// &lt;/summary&gt; /// &lt;param name="context"&gt;The DbContext on which to run the initialiser&lt;/param&gt; public void InitializeDatabase(TContext context) { var exists = context.Database.Exists(); try { if (exists &amp;&amp; context.Database.CompatibleWithModel(true)) { // everything is good , we are done return; } if (!exists) { context.Database.Create(); } } catch (Exception) { //Something is wrong , either we could not locate the metadata or the model is not compatible. if (exists) { context.Database.ExecuteSqlCommand("ALTER DATABASE Pariksha SET SINGLE_USER WITH ROLLBACK IMMEDIATE"); context.Database.ExecuteSqlCommand("USE Master DROP DATABASE Pariksha"); context.SaveChanges(); } context.Database.Create(); } } } </code></pre> <p>something about the above code is more than just hacky (Feel free to chip in with help)</p> <p>I then added migrations and got the migration script to work correctly as well.</p> <pre><code> internal sealed class Configuration : DbMigrationsConfiguration&lt;ParikshaContext&gt; { public Configuration() { AutomaticMigrationsEnabled = false; ContextKey = "EFRepository.Context.ParikshaContext"; } protected override void Seed(ParikshaContext context) { } } </code></pre> <p>the migrations work as expected.</p> <p>Now, the questions is in my application startup what should I do ? Something like this </p> <pre><code> var config = new Configuration(); var migrator = new DbMigrator(config); migrator.Update(); </code></pre> <p>and some forums suggested this as well in the constructor which seems a little odd because I don't want to check if the db and schema are correct everytime I use the Context. So, what could be the possible use of this technique or did I get the context of the suggestion as wrong ?</p> <pre><code>public ParikshaContext() : base("Pariksha") { Database.SetInitializer(new ParikshaDataBaseInitializer&lt;ParikshaContext&gt;()); } </code></pre> <p>To summarise, </p> <ol> <li><p>what is the correct use-case for the different techniques available ?</p></li> <li><p>what would be the ideal strategy so that the migrations work in all conditions and when we move databases from one environment to another ?</p></li> </ol>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload