Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It seems there are a lot of ways to configure entityframework and everyone has there own take on what is best. All I can offer is my take based on what we've standardized at my work. A lot of this is developer preference. My preference happens to be controlling as much as possible so I always understand exactly what is happening and when.</p> <h2>Automatic Migrations</h2> <p>First off, while Automatic Migrations may be convenient but they cause a lot of trouble particularly as a project grows and/or data becomes more complex. In my opinion any commercial/production system should have more control than this. We always turn off automatic migrations for all of our major projects by setting <code>AutomaticMigrationsEnabled = false;</code>. We run our migrations explicitly when we want it done (on dev this is in the package manager console in visual studio by typing <code>Update-Database</code> and in production we have written our own little migration utility that just calls the migrate to latest code explicitly - but none are automatic).</p> <p>@Terric's answer scares me with both automatic migrations AND data loss being permitted! I don't want to be the guy who deploys a solution and wipes out some important data because of a badly executed column alteration that resulted in data loss. As a side note when we run out migration explicitly in dev I often use the -v switch for verbose ouptut (<code>Update-Database -v</code>). This lets you see the SQL being executed and any failures/warnings if appropriate.</p> <p>It has also been our experience that changing these settings after you are several migrations into development doesn't go well. I'm not sure where this is being tracked, but starting a project fresh with automatic migrations disabled ensures nothing unexpected is going to happen.</p> <p>Personally, I'd remove the Initializer you have <code>MigrateDatabaseToLatestVersion</code> and run the migrator myself exactly when I want to (either via the package manager console or via my own explicit code somewhere).</p> <h2>Creating a database if it doesn't exist</h2> <p>This behavior is provided by a DatabaseInitializer (not really EntityFramework itself). The <code>CreateDatabaseIfNotExists</code> initializer is built into EntityFramework and a default in some versions. However, again I'm not one for all the inferred app behavior stuff. In my opinion I'd like a little more control.</p> <p><a href="http://peterkellner.net/2012/02/17/entityframework-codefirst-4-3-adding-data-migration-to-simple-example-part-2/">This guy has an example of a custom database initializer inheriting from the built in CreateDatabaseIfNotExists</a>. But you could always just create your own and implement whatever exact logic you want to see (including the creation of your database). Again this just avoids unexpected behavior. My personal preference as a developer is to control this stuff closely unless I'm just goofing around with a mockup or test project.</p> <p>Super simple custom DatabaseInitializer with no unexpected behavior:</p> <pre><code>namespace MyProject.Data.DatabaseInitializers { public class MyCustomDbInit&lt;TContext&gt; : IDatabaseInitializer&lt;TContext&gt; where TContext : DbContext { public void InitializeDatabase(TContext context) { // Create our database if it doesn't already exist. context.Database.CreateIfNotExists() // Do you want to migrate to latest in your initializer? Add code here! // Do you want to seed data in your initializer? Add code here! } } } </code></pre> <h2>The Results</h2> <p>If you use a code first approach, disable automatic migrations and use a custom DatabaseInitializer like the above, you will have very good control over what is happening and when.</p> <p>We use these strategies at work and have zero issues (although it did take some trouble to settle on these strategies). Hopefully you will find similar success!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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