Going Portable: How can I keep code-first database with development project on USB drive

Answered Going Portable: How can I keep code-first database with development project on USB drive

  • Thursday, November 29, 2012 3:58 PM
     
     

    I'm starting to delve into the entiy framework and creating my first "non tutorial" project.  I go back and forth between developing on a desktop PC and a laptop.  Both machines have VS 2012. 

    So what I'm looking for is a connection string that would allow me to keep both my source project *and* my development database on my USB drive.  At this point in my learning, the database need only be attached when I'm working.

    Any suggestions?


    This signature unintentionally left blank.

All Replies

  • Friday, November 30, 2012 6:54 AM
     
     

    Hi Nick,

    I am not sure whether you want to know how to store your two databases on USB device. Or, you want to know how to connect to databases which stored on USB device.

  • Friday, November 30, 2012 4:20 PM
     
     

    Let me try and explain better.  My visual studio solution is on my usb drive.  \dev\MyProject\MyProject.sln.  At this point I haven't done anything to customize my .config file.

    When I'm working on my desktop, I open this as H:\dev\MyProject\MyProject.sln.  When I run my console application, it connects to (localdb)\v11.0 and creates\uses a database on the desktop. The actual data file is somewhere on the desktop C:\ drive.

    When I'm working on my laptop, I open this as E:\dev\MyProject\MyProject.sln.  When I run my console application, it connects to (localdb)\v11.0 and creates\uses a database on the laptop. The actual data file is somewhere on the laptop C:\ drive.

    This is inconvenient, so I want to specify a connection--either thru code or in the configuration file--that uses an MDF file that can live on the USB drive.  I know for Web applications I can specify the attribute "AttachDbFilename=|DataDirectory|\SomeFile.mdf" and it will load the file from the website's App_Data folder.

    But right now I'm playing with a console application while I learn and refine my data model.  I'm looking for a way to load it from what essentially amounts to $(SolutionDir)MyProject.mdf

    Does this make it more clear?


    This signature unintentionally left blank.

  • Friday, November 30, 2012 10:06 PM
     
     Answered Has Code

    Seems like I came up with a viable opiton.  Not exactly pretty, but lets me work at my desk or on sofa at will :-)

        class Program
        {
            static void Main(string[] args)
            {
    #if (DEBUG)
                /*
                 * For convenience of development assume data will be placed in the
                 * same location as EXE.  Setting "DataDirectory" here makes localdb
                 * use that location by default. (just like a web app)
                 */
                var dir = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
                AppDomain.CurrentDomain.SetData("DataDirectory", dir);
                Trace.TraceInformation("DataDirectory = " + dir);
    #endif    
            
                // ... 
            }
        }
    


    This signature unintentionally left blank.

    • Marked As Answer by Nick F. _ Friday, November 30, 2012 10:07 PM
    •