efcore icon indicating copy to clipboard operation
efcore copied to clipboard

Workaround for no Automatic Migrations [Question]

Open GVX111 opened this issue 11 years ago • 7 comments

How to run migration without restart application? I'm creating migration with this code:

var history = this.GetInfrastructure().GetService();
            var options = this.GetInfrastructure().GetService();
            var migrate = this.GetInfrastructure().GetService();
            var modelDiffer = this.GetInfrastructure().GetService();
            var providerServices = this.GetInfrastructure().GetService();

            
            var idGenerator = new MigrationsIdGenerator();
            var code = new CSharpHelper();
            var c = new MigrationsScaffolder(
                 this,
                 this.Model,
                 new MigrationsAssembly(
                     this,
                     options,
                     idGenerator),
                 modelDiffer,
                 idGenerator,
                 new CSharpMigrationsGenerator(code, new CSharpMigrationOperationGenerator(code), new CSharpSnapshotGenerator(code)),
                 history,
                 new LoggerFactory(),
                 providerServices);
            var migratename = Guid.NewGuid().ToString();
            var m = c.ScaffoldMigration(migratename, this.GetType().Namespace);

but i need to execute this Migration at runtime. @bricelam @mikary @rowanmiller

GVX111 avatar Nov 15 '15 21:11 GVX111

Repro [http://www.megafileupload.com/5ups/ContentStore.rar]

GVX111 avatar Nov 16 '15 19:11 GVX111

Could you describe more about the scenario you're trying to enable? How does the model change at runtime? Could you simply drop and recreate the database schema? Would EF6's automatic migrations be more appropriate for your application?

bricelam avatar Nov 16 '15 19:11 bricelam

@bricelam I want automatic migration on Model Changing. If i add some property of model i want to change also Db table without data loss. if have time look at my repro and you will understand what i want. 5 day i'm trying to do this scenario and i can't continue my work. :(

GVX111 avatar Nov 16 '15 19:11 GVX111

Hmm... I believe this goes against our current design of the Migrations feature in EF7. If you really want to, you could write some code to compile the generated *.cs files into a dynamic assembly, create a custom implementation of IMigrationsAssembly that also looks at the types in that assembly, then use Migrator to apply the migration at runtime...

However, my recommendation would be to either...

  • Use EF6
  • Change your development workflow to ensure there aren't any pending model changes. This could be done in a variety of ways:
    • A unit test
    • A build task
    • An exception during app start

The ASP.NET 5 EntityFramework error page includes code to check for pending model changes.

bricelam avatar Nov 16 '15 22:11 bricelam

@bricelam I'm generation migration (*.cs) with MigrationsScaffolder.Save() but this cs is not loaded in Assembly. Can you give me some example how to do?

GVX111 avatar Nov 17 '15 06:11 GVX111

I did but problem is also migration. First i create News Table after that i removed one property (Column) from this model and migration can't update Table. Repro: https://www.mediafire.com/?digit4g5xr1a6x8 Please look in this project its console application.

An exception of type 'System.Data.SqlClient.SqlException' occurred in EntityFramework.Relational.dll but was not handled in user code
Additional information: There is already an object named 'News' in the database.

GVX111 avatar Nov 17 '15 11:11 GVX111

Discussed in team triage and we concluded that we do not want to support automatic migration scenarios in EF7. We saw them cause lots of headaches for folks in EF4.1-6 where they handled the first few scenarios ok but then just break down as things get more complex.

As noted in this issue, it should be possible to plug all the migrations components together to do something similar if you really want to (though we would advise against it unless your scenarios are limited to adding new properties).

While attempting to do this, if you hit cases where APIs need to be reworked to facilitate the scenario, then we'd be happy to take specific issue where we can change things (or feel free to submit a pull request for them).

rowanmiller avatar Nov 20 '15 18:11 rowanmiller