none
IDbContextFactory - why is there almost zero documentation or help on this feature? RRS feed

  • Question

  • Not sure if there is some obscure part of the interwebs where this info is kept but this is really "light on the ground"...

    I've got a DB context that gets some stuff passed in to the constructor...

    EF complains that I dont' have a parameterless constructor, so I create one and shove a bunch of sensible defaults in the "stuff passed in" places...

    but then the Migrations happen...

    so then I want these things to be passed in, so I remove the constructor and follow the "create the implementation of IDbContextFactory in the same assembly as the derived context" as instructed...

    The context factory type 'MyMigrationsContextFactory' does not have a public parameterless constructor. Either add a public parameterless constructor, create an IDbContextFactory implementation in the context assembly, or register a context factory using DbConfiguration.

    From this it just looks like I've shoved the problem down a class... sigh... what am I missing...?

    Is there some guidance somewhere? How am I supposed to get STUFF into this factory? or MUST I have parameterless constructor...?


    - sure I'm noJedi but that's no reason to stop trying to make stuff levitate! -

    Wednesday, April 15, 2015 3:09 PM

All replies

  • Hello noJedi,

    >>Is there some guidance somewhere? How am I supposed to get STUFF into this factory? or MUST I have parameterless constructor...?

    It sounds like you are trying to use this IDbContextFactory interface, if so, you could check this link: http://stackoverflow.com/a/11748370, it provides an example.

    If not, please tell us what you are trying to do and provided the related code.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, April 16, 2015 2:48 AM
    Moderator
  • Hi there,

    Unfortunately I already found that... and had gotten that far already by myself unfortunately.

    Basically what I want is this:

    MyContext:DBContext

    > ctor(Ilogger logger)

    So this is pretty basic

    but when you introduce Migrations it wants the static constructor

    eg:

     static MyContext()
            {
                Database.SetInitializer(new MigrateDatabaseToLatestVersion<BankPortalContext, MyEFDB.Migrations.Configuration>());
            }

    This (as I understand it) helps the context check if its at the right migration and applies changes and so on,

    however when this process is done there is no "instance" so its created (and throws error if you don't have parameterless constructor - or if you have both, then you have NULL logger because there is no way to pass in the logger instance).

    Now I COULD just dirty my classes with "if logger is null then get one" or something, but I thought it would be nice to know how this was supposed to work...

    Am I supposed to give IDBContextFactory some knowledge of a IOC container, or the Logger, or is the Factory just supposed to have the "dirty bit" and get a logger from some static Logger implementation... etc...

    Thanks in advance.


    - sure I'm noJedi but that's no reason to stop trying to make stuff levitate! -

    Friday, April 17, 2015 8:38 AM
  • I think the resource would be the best learn document:https://entityframework.codeplex.com/SourceControl/latest

    You could down it and look into its inside implementation.

    Monday, April 20, 2015 12:45 PM
  • Hi there,

    nice idea, however my key issue is "how to USE it", not "how it WORKS" which is all the code would reveal, not the intended use of it.

    Not a big deal, I'll just get a hammer and make it work regardless of what issues it might cause me further down the track.

    Thanks for trying regardless. :)


    - sure I'm noJedi but that's no reason to stop trying to make stuff levitate! -

    Wednesday, April 22, 2015 12:05 AM
  • Hello noJedi,

    Please check links below to see if helpful:

    How to implement IDbContextFactory for use with Entity Framework data migrations

    IDbContextFactory which gets Connection String at runtime and triggers Automatic Migrations


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Saturday, April 25, 2015 2:23 AM
    Moderator