.net - Get local migrations from assembly using EF Code First without a connection string -


so i'm stuck following situation, maybe guys can shed light on this:

scenario
our application exists of many client applications (winforms), each of use specific version of database model using ef code first migrations. client application has ability connect several different databases, support required schema client needs.

next have 1 central service application. application responsible maintaining databases (creating/removing), , returning connection strings clients need access. client can send request central service create new database. 1 of arguments of call schema version needs (as string). problem lies:

problem
problem find out highest version of migration is, without providing connection string or database connect to. 1 use:

var configuration = new migrationsconfiguration() { contexttype = typeof(corecontext) }; var migrator = new dbmigrator(configuration); var versions = migrator.getlocalmigrations(); 

but automatically results connecting database under hood (if not provided, .\sqlexpress). behavior happens when explicitly set databaseinitializer null , provide dbmigrationsconfiguration implementation automaticmigrations set false.

trying follow stacktrace see following:

at system.data.entity.utilities.dbproviderservicesextensions.getprovidermanifesttokenchecked(dbproviderservices providerservices, dbconnection connection) @ system.data.entity.infrastructure.defaultmanifesttokenresolver.<>c__displayclass1.<resolvemanifesttoken>b__0(tuple`3 k) @ system.collections.concurrent.concurrentdictionary`2.getoradd(tkey key, func`2 valuefactory) @ system.data.entity.infrastructure.defaultmanifesttokenresolver.resolvemanifesttoken(dbconnection connection) @ system.data.entity.utilities.dbconnectionextensions.getproviderinfo(dbconnection connection, dbprovidermanifest& providermanifest) @ system.data.entity.dbmodelbuilder.build(dbconnection providerconnection) @ system.data.entity.internal.lazyinternalcontext.createmodel(lazyinternalcontext internalcontext) @ system.data.entity.internal.retrylazy`2.getvalue(tinput input) @ system.data.entity.internal.lazyinternalcontext.initializecontext() @ system.data.entity.internal.lazyinternalcontext.get_modelbeinginitialized() @ system.data.entity.infrastructure.edmxwriter.writeedmx(dbcontext context, xmlwriter writer) @ system.data.entity.utilities.dbcontextextensions.<>c__displayclass1.<getmodel>b__0(xmlwriter w) @ system.data.entity.utilities.dbcontextextensions.getmodel(action`1 writexml) @ system.data.entity.utilities.dbcontextextensions.getmodel(dbcontext context) @ system.data.entity.migrations.dbmigrator..ctor(dbmigrationsconfiguration configuration, dbcontext userscontext) @ system.data.entity.migrations.dbmigrator..ctor(dbmigrationsconfiguration configuration) 

this gets little rocket science-like me. maybe can explain me whether it's possible or not work ef code first models without providing connection string or database. or maybe i'm missing crucial configuration setting?

regards, patrick

ps. found workaround using reflection of assembly/namespace in migrations resides, , selecting id's. how getlocalmigrations() works.

well, answered own question confirm suspicion: cannot local migraitons dbmigrator class without providing target database.

regarding solution using reflection , right original approach uses in migrationassembly class, simplified version can use code this:

var migrationssimplified = (from t in migrationsassembly.definedtypes.select(t => t.astype())                             t.issubclassof(typeof(dbmigration))                             select (imigrationmetadata)activator.createinstance(t))                             .orderby(mm => mm.id)                             .tolist(); 

the original function can found here: http://entityframework.codeplex.com/sourcecontrol/latest#src/entityframework/migrations/infrastructure/migrationassembly.cs

and other part of question, if go backwards on stack trace of exception find

system.data.entity.utilities.dbcontextextensions.getmodel(this dbcontext context) 

function find here: http://entityframework.codeplex.com/sourcecontrol/latest#src/entityframework/infrastructure/edmxwriter.cs use context type specified in dbmigrationsconfiguration.

and if go deeper find getproviderinfo info in dbconnectionextensions class wants connection haven't defined.

one thing note: exception not occure unti call getlocalmigrations functions because context uses lazy initialization.


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

thorough guide for profiling racket code -