Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Warren Buckley 2106 posts 4836 karma points MVP 7x admin c-trib
    Oct 14, 2013 @ 17:00
    Warren Buckley
    0

    WinForm - Using Umbraco ApplicationContext.Current

    Hello all,
    I am currently updating the Visual Studio Package for the File --> New Project extension for Umbraco to use a Wizard and this case it is a simple WinForm that loads which asks the user which mode to use (MVC or WebForms) and the DB choice SQL CE or custom DB connection string..

    I have it mostly working but I am currently stuck on updating the DB by using the Umbraco.Application.Current methods in the Database Service like so:

    if (Umbraco.Core.ApplicationContext.Current != null)
    {
      Umbraco.Core.ApplicationContext.Current.DatabaseContext.ConfigureEmbeddedDatabaseConnection();
    }

    Or alternatively this:

    //Use Umbraco DB API - to update web.config
    if (Umbraco.Core.ApplicationContext.Current != null)
    {
       //Setup DB config
       Umbraco.Core.ApplicationContext.Current.DatabaseContext.ConfigureDatabaseConnection(dbConnectionString);
       //Use API to create DB schema... (Skips Web Installer)
       Umbraco.Core.ApplicationContext.Current.DatabaseContext.Database.CreateDatabaseSchema();
    }


    However my current issue is that the Current ApplicationContext is null so I am unable to use these awesome DB calls. Does anyone have any pointers or guidance for me.

    Thanks,
    Warren :)

  • Stephen 767 posts 2273 karma points c-trib
    Oct 14, 2013 @ 17:02
    Stephen
    0

    You want to look at Umbraco.Web.Standalone which creates a context for such standalone apps. It's all internal at the moment, though.

  • Warren Buckley 2106 posts 4836 karma points MVP 7x admin c-trib
    Oct 14, 2013 @ 17:05
    Warren Buckley
    0

    Hey Stephen.

    So it's not possible to do then? I have seen Morten C create a command line application that uses the DB context before but not sure how to apply it to WinForms.

    https://github.com/sitereactor/umbraco-console-example

    (I am not a WinForms developer at all :S )

  • Stephen 767 posts 2273 karma points c-trib
    Oct 14, 2013 @ 17:08
    Stephen
    0

    Morten's command line app was using internal stuff as far as I know. Umbraco.Web.Standalone was inspired by his app. But none of this is open at the moment because it's not finalized... If you want to look at it and see if it's polished enough we might open it, nothing against it I guess.

    But not tonight ;-(

  • Warren Buckley 2106 posts 4836 karma points MVP 7x admin c-trib
    Oct 14, 2013 @ 17:19
  • Lars-Erik Aabech 349 posts 1100 karma points MVP 8x c-trib
    Oct 14, 2013 @ 19:12
    Lars-Erik Aabech
    0
    I see :) (just jumping in from twitter)
    I've done that, but the project got shelved ATM due to sites going live and the code-first project.
     
    But here's a gist that _should_ give you an installed DB. However I only tested it with SQL Server.
     
     
    Be aware that the code may well break as Umbraco gets changed.
    But I'm sure the core guys are well aware of the troubles we are having with coupling and internals, and will hopefully address it when the U7 mayhem is done. :)
     
    Lars-Erik (@bleedo)
  • Lars-Erik Aabech 349 posts 1100 karma points MVP 8x c-trib
    Oct 14, 2013 @ 23:00
    Lars-Erik Aabech
    0

    I've been fiddling a bit with your project and I've got some good, and some bad news. :)

    The bad: The DatabaseContext is too tightly coupled to different parts of Umbraco, which all has to be reflected.
    The good: However, there's not that much of the logic you really need to use which is actually coupled.

    It's gonna take a little while to test and get right (and I'm tired), so I'd like to jump off again for now, but here's what I'd do if I were you:

    - Get a clone of Umbraco
    - Create your own "DatabaseContext" class in your project, and copy the following methods from Umbraco.Core.DatabaseContext:
        - SaveConnectionString
            - Remove lines 242-247, and make sure you'll pass in a providerName
            - Remove lines 252 to 260, replace vDir with a parameter where you pass your own path to the root
        - ConfigureEmbeddedDatabaseConnection
            - Add path parameter to pass to SaveConnectionString
            - Replace GlobalSettings.FullpathToRoot in line 119 with your path
            - Remove call to initialize
        - ConfigureDatabaseConnection
            - Add path parameter to pass to SaveConnectionString

    I might have missed something from browsing the code, but it should be the bulk of it. You've basically just replicated "simple" BCL usage from Umbraco, instead of getting "stuck" trying to re-use something not intended as an API.

    When that class has been used to create the CE DB and/or set the connectionstring to the DB, you can just instantiate Umbraco.Core.Persistence.Database with a regular connection to the given DB. It, and it's CreateDatabaseSchema() method is public and free to use. (bless it's author)

    Hope this helps you guys. :)

    L-E

  • Warren Buckley 2106 posts 4836 karma points MVP 7x admin c-trib
    Oct 15, 2013 @ 09:45
    Warren Buckley
    0

    Thanks for the notes & ideas Lars.

    Looks like I will be doing alot of copying & pasting code from the core today :-P

    Cheers,
    Warren 

Please Sign in or register to post replies

Write your reply to:

Draft