Asp.Net core 2.0: Detect Startup class invoked from migration or other ef operation -
at current moment default startup.cs
flow executed on every db related operation droping db, adding migration, updating db migrations, etc.
i have heavy app specific code in startup
need invoked if application run real. how detect startup
class run migration or other database related dotnet
command.
well, noticed in comment question there idesigntimedbcontextfactory
interface need implemented resolve dbcontext
@ design time.
it this:
public static class programm{ ... public static iwebhost buildwebhostduringgen(string[] args) { return webhost.createdefaultbuilder(args) .usestartup<startupgen>() // <--- i'm using different startup child there less complex code .usedefaultserviceprovider(options => options.validatescopes = false).build(); } } public class dbcontextfactory : idesigntimedbcontextfactory<mydbcontext> { public mydbcontex createdbcontext(string[] args) { return program.buildwebhostduringgen(args).services.getrequiredservice<mydbcontext>(); } }
however, due unclear reasons (i asked guys microsoft, don't explain me) dotnet
on every operation implicitly call programm.buildwebhost
if it's private - that's reason why standard flow executed each time question's author. workaround - rename programm.buildwebhost
else, initwebhost
there issue created that, maybe resolved in 2.1 release on in future.
Comments
Post a Comment