6

Following on from How do I inject a connection string into an instance of IDbContextFactory? and several basically similar questions, I am wondering what the point of IDbContextFactory<T> is for Entity Framework migrations.

According to MSDN IDbContextFactory<T> is all about design-time support, which doesn't really seem particularly relevant to Code-First Migrations.

Any factory classes implemented must implement a default constructor themselves (which is the one that EF/ Migrations uses) and must implement the generic T Create() method. This just seems a repeated abstraction, I can't see it adds anything (maybe this is necessary for design-time approaches.)

This means there is no way of doing any constructor-injection, which means I can't really use this in reality, as various parameters (including, but not limited to the nameOrConnectionString aren't known until runtime). See also Entity Framework 4.3.1 migrations always invokes default constructor and ignores connectionstring and Manually Provide DbContext to DbMigrator. A bunch of hacks.

With EF in particular, IoC at all and Dependency Injection specifically always seems to be an after-thought, if there at all.

Community
  • 1
  • 1
nicodemus13
  • 2,258
  • 2
  • 19
  • 31
  • Not really, sorry. I'm using `FluentMigrations` instead as it's much more flexible and mature (for one, I don't see how one can easily to data-migration in EF Migrations- it's all about schema, which is useless if I can't upgrade the data as well). In the end I just didn't inject as it wasn't critical to do so. – nicodemus13 Apr 02 '14 at 15:36
  • 1
    In 2018, have you found a solution for this yet? It is insanely frustrating that the EF team wouldn't provide a means of injecting the connection string, name, or a provider into the IDbContextFactory. I, like you, see absolutely no use for this interface class. Since we can't inject anything into the factory, we are really just moving where the connection string details are defined...which is actually quite worse than putting it in the context since it adds hidden complexity. What a joke. – crush Sep 27 '18 at 20:04
  • 1
    To rub salt in the wound the `Enable-Migrations` command allows a connection string, name, or provider to be passed as a command-line argument. The problem is that there's no way to pass that along to this worthless factory. EF should be injecting some kind of migrations configuration object at a minimum, if not providing us a means of wiring up our own dependency injection. – crush Sep 27 '18 at 20:07

0 Answers0