You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
At the beginning of EF Core we decided that we would use the fluent API in model snapshots rather than designing a new serialization format. (In other words, the fluent API is the way to serialize the model.) However, this can lead to a lot of code compiled for big models and when the number of migrations build up. See, for example, #18618.
Some more extensive overhaul of the migrations experience
Reacted by viniciusverasdossantos, Viktor Svub, Greg Bachraty, Alex Tsoi, Glen, Paul DeVito, ghosttie, austinw-fineart, MoishyS, Benoit Gerard and 12 more
Note that this will likely mean that snapshots will need to be updated for major version upgrades and possibly minor ones. So we should consider #18557 at the same time
Another idea I has was to only put the store model in the *.Designer.cs files, but this doesn't work since we need the full model when removing a migration.
I was trying to work around this in our product that have ~440 migrations and we can't squash them into a single to get a fresh start because the product is installed on-premises at customer and different customer is in different stage of upgrades in versioning.
I come up to the solution where I created a MSBuild task that extracting the attributes from the *.Designer.cs files and generated a new file. Then I adding a targets file that excluding all the *.Designer.cs files and including the generated file. This somehow is working in many cases but some CreateIndexOperations will add an filter [Id] IS NOT NULL even on non-nullable properties and this does not happen when running the original code.
The generated file does not contain the BuildTargetModel file where all the metadata about the model at that time the model was created is ignored, this reduced the size for the built assembly from 32 MB to 2 MB and build time from 2 minutes and 30 seconds to 8 seconds so that is a good improvements in speed.
Does it exists any information about how the model in BuildTargetModel is used during migration up command?
Should it work to run the migrations up command without the model in BuildTargetModel ?
Does it exists any information about how the model in BuildTargetModel is used during migration up command?
This answer is old and there are probably more cases now, but it should give you an idea of how the model is used..
This was what I needed to be able to discard this idea, but I investigated how the migrations happens inside EFCore and this is the new working solutions, sharing if anyone else have the same need.
Part 1:
First I created a new class library Company.Application.Migrations.Archive that haven't any reference to the original code and having this in my csproj file
Then I moved all migrations files into this project, the designer file have a reference to the type of the db context so I created a new db context with the same name in this assembly as below.
A lot of migrations have still references in usings to namespaces in the main application but was not using any classes in them so created a large file as
This project is now building and a NuGet packages is created.
Part 2:
In the main project I adding a package reference to the NuGet package from part 1, then I creating a decorator that wrapping the default IMigrationsAssembly as the following that wrapping the current assembly with the archived assembly and running all the migrations. For service decoration I using something similar to https://github.com/khellang/Scrutor but with attributes.
[ServiceDecorator(typeof(IMigrationsAssembly))]internalclassMigrationsAssemblyDecorator:IMigrationsAssembly{privatereadonlyIMigrationsAssembly_archiveMigrationAssembly;privatereadonlyIMigrationsAssembly_parent;publicIReadOnlyDictionary<string,TypeInfo>?_migrations=default;publicIReadOnlyDictionary<string,TypeInfo>Migrations{get{return_migrations??=_archiveMigrationAssembly.Migrations.Concat(_parent.Migrations).ToDictionary().AsReadOnly();}}publicModelSnapshotModelSnapshot=>_parent.ModelSnapshot;publicAssemblyAssembly=>_parent.Assembly;[System.Diagnostics.CodeAnalysis.SuppressMessage("Usage","EF1001:Internal EF Core API usage.",Justification="<Pending>")]publicMigrationsAssemblyDecorator(IMigrationsAssemblyparent,IMigrationsIdGeneratoridGenerator,IDiagnosticsLogger<DbLoggerCategory.Migrations>logger){varcurrentContext=Archive.ArchivedDbContextAccessor.GetArchivedDbContext();varobuilder=newDbContextOptionsBuilder<CompanyDbContext>();varsbuilder=newSqlServerDbContextOptionsBuilder(obuilder);sbuilder.MigrationsAssembly(currentContext.Context.GetType().Assembly);vararchiveOptions=obuilder.Options;_archiveMigrationAssembly=newMigrationsAssembly(currentContext,archiveOptions,idGenerator,logger);_parent=parent;}publicMigrationCreateMigration(TypeInfomigrationClass,stringactiveProvider){return_parent.CreateMigration(migrationClass,activeProvider);}publicstringFindMigrationId(stringnameOrId){return_archiveMigrationAssembly.FindMigrationId(nameOrId)??_parent.FindMigrationId(nameOrId);}}
Now, when using the IMigrationsAssembly the methods have access to migrations from the class library from the NuGet package and the migrations in the main assembly.
Activity
AndriySvyryd commentedon Oct 30, 2019
Note that this will likely mean that snapshots will need to be updated for major version upgrades and possibly minor ones. So we should consider #18557 at the same time
bricelam commentedon Nov 1, 2019
Another idea I has was to only put the store model in the *.Designer.cs files, but this doesn't work since we need the full model when removing a migration.
ajcvickers commentedon Oct 28, 2021
We believe this is best handled by squashing migrations: #2174
ajcvickers commentedon Mar 12, 2023
Re-open to consider using the relational model.
Tasteful commentedon Mar 11, 2025
I was trying to work around this in our product that have ~440 migrations and we can't squash them into a single to get a fresh start because the product is installed on-premises at customer and different customer is in different stage of upgrades in versioning.
I come up to the solution where I created a MSBuild task that extracting the attributes from the
*.Designer.csfiles and generated a new file. Then I adding a targets file that excluding all the*.Designer.csfiles and including the generated file. This somehow is working in many cases but someCreateIndexOperationswill add an filter[Id] IS NOT NULLeven on non-nullable properties and this does not happen when running the original code.The generated file does not contain the
BuildTargetModelfile where all the metadata about the model at that time the model was created is ignored, this reduced the size for the built assembly from 32 MB to 2 MB and build time from 2 minutes and 30 seconds to 8 seconds so that is a good improvements in speed.Does it exists any information about how the model in
BuildTargetModelis used during migration up command?Should it work to run the migrations up command without the model in
BuildTargetModel?bricelam commentedon Mar 11, 2025
This answer is old and there are probably more cases now, but it should give you an idea of how the model is used..
Tasteful commentedon Mar 12, 2025
This was what I needed to be able to discard this idea, but I investigated how the migrations happens inside EFCore and this is the new working solutions, sharing if anyone else have the same need.
Part 1:
First I created a new class library
Company.Application.Migrations.Archivethat haven't any reference to the original code and having this in my csproj fileThen I moved all migrations files into this project, the designer file have a reference to the type of the db context so I created a new db context with the same name in this assembly as below.
Also created a simple way to create the db context with an accessor class
A lot of migrations have still references in usings to namespaces in the main application but was not using any classes in them so created a large file as
This project is now building and a NuGet packages is created.
Part 2:
In the main project I adding a package reference to the NuGet package from part 1, then I creating a decorator that wrapping the default
IMigrationsAssemblyas the following that wrapping the current assembly with the archived assembly and running all the migrations. For service decoration I using something similar to https://github.com/khellang/Scrutor but with attributes.Now, when using the
IMigrationsAssemblythe methods have access to migrations from the class library from the NuGet package and the migrations in the main assembly.