How to register a custom EF Code First migration operation for scaffolding?


Question

I have class SetIdentitySeedOperation: MigrationOperation, which sets the identity value for the specified table. To wire it up, I use a custom SqlServerMigrationSqlGenerator, so if I manually write .AddOperation(new SetIdentitySeedOperation(...)) in my migration's void Up(), my Generate method gets called, and I generate the appropriate SQL there.

What's the mechanism to register this custom operation with the migration scaffolding, so that the code to add this operation gets generated on Add-Migration?


UPDATE: I've been looking at the EF migrations source code, and it seems that the list of the migration operations to get scaffolded comes from a call to EdmModelDiffer.Diff(), and EdmModelDiffer is internal, so there's seemingly no way to plug your own conventions or attributes into the scaffolding engine.

In light of this, the existence of the public surface for the migration operations and SQL generation seems rather pointless, as it provides no true extensibility point for the library users, requires the user to manually write explicit calls to their custom migration operations, and that's after having to write some boilerplate code to wrap their SQL generation into an operation.

So is this entire mechanism only useful when the same code targets different databases?

Popular Answer

This is now possible with EF 6.1:

Code First Annotations allow you to set annotations on tables and columns and then hook into the scaffolding operations by extending SqlServerMigrationSqlGeneratorand change the scaffolding based on those attributes you set.





Licensed under: CC-BY-SA
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why