简体   繁体   中英

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

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?

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 SqlServerMigrationSqlGenerator and change the scaffolding based on those attributes you set.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM