dal codice sorgente di DbMigrationsConfiguration<TContext>
:
/// <summary>
/// Runs after upgrading to the latest migration to allow seed data to be updated.
///
/// </summary>
///
/// <remarks>
/// Note that the database may already contain seed data when this method runs. This means that
/// implementations of this method must check whether or not seed data is present and/or up-to-date
/// and then only make changes if necessary and in a non-destructive way. The
/// <see cref="M:System.Data.Entity.Migrations.DbSetMigrationsExtensions.AddOrUpdate``1(System.Data.Entity.IDbSet{``0},``0[])"/>
/// can be used to help with this, but for seeding large amounts of data it may be necessary to do less
/// granular checks if performance is an issue.
/// If the <see cref="T:System.Data.Entity.MigrateDatabaseToLatestVersion`2"/> database
/// initializer is being used, then this method will be called each time that the initializer runs.
/// If one of the <see cref="T:System.Data.Entity.DropCreateDatabaseAlways`1"/>, <see cref="T:System.Data.Entity.DropCreateDatabaseIfModelChanges`1"/>,
/// or <see cref="T:System.Data.Entity.CreateDatabaseIfNotExists`1"/> initializers is being used, then this method will not be
/// called and the Seed method defined in the initializer should be used instead.
///
/// </remarks>
/// <param name="context">Context to be used for updating seed data. </param>
Fondamentalmente, non hanno un'altra opzione di implementare una logica "aggiungere o aggiornare", perché il metodo di Seed verrà eseguita ogni volta dopo l'inizializzazione viene utilizzato .
Il metodo di estensione AddOrUpdate è utile per questo, ma ho anche usato questo in alcuni casi:
if (!context.Entities.Any())
{
// Seed
}
fonte
2015-02-20 19:10:28
Stai utilizzando il metodo di estensione 'AddOrUpdate'? – octavioccl
Nop, non sto usando il 'AddOrUpdate' per tutti gli inserimenti di seed, accanto ci sono gli script slq raw. –
http://stackoverflow.com/a/20245687/150342 – Colin