none
How to replace connection string for Sqlite database? RRS feed

  • Question

  • I am trying to use EF with sqlite.  Sqlite is a desktop database that can reside anywhere.  It's just a file.  In my application, I allow the user to create a project database anywhere on the file system.  The problem is getting EF to use the correct database. Following "Programming Entity Framework", I have created a function, where location is the full path to the sqlite database file:

    private static myfirstEntities getNewCString(string location)
    {
     var cstring = ConfigurationManager.ConnectionStrings["myfirstEntities"].ConnectionString;
     var connectionStringBuilder = new EntityConnectionStringBuilder(cstring);
     connectionStringBuilder.ProviderConnectionString = String.Format("data source={0}",location);
     return new myfirstEntities(connectionStringBuilder.ConnectionString);
    }

    My problem is the very last line.  It keeps coming back with "Unable to load the specified metadata resource" error.

    It does this regardless of what the location is.  In fact it even does this when I don't change the location!

    Is this the correct process to change EF connection string?  What is causing the error?

    Win7, VS2010, 64 bit.


    Robotuner

    Saturday, August 4, 2012 4:21 AM

Answers

  • Turns out what I was doing was correct.  I had a typo in my code.  Referring to my initial posting, myfirstEntities is the EF entity that was created by VS with a database in a known location.

    var cstring = ConfigurationManager.ConnectionStrings["myfirstEntities"].ConnectionString;

    gets the config string from the app.config file.

    var connectionStringBuilder = new EntityConnectionStringBuilder(cstring);

    uses the cstring to construct the same EntityConnection that EF would.

    connectionStringBuilder.ProviderConnectionString = String.Format("data source={0}",location);

    substitute the actual database location in the EntityConnection Object.

    and

    return new myfirstEntities(connectionStringBuilder.ConnectionString);

    Creates the EntityContext with the revised string.


    Robotuner

    • Marked as answer by Robotuner Tuesday, August 7, 2012 1:22 AM
    Tuesday, August 7, 2012 1:22 AM

All replies

  • Hi Robotuner,

    Do you use Code First? If you are using Database First, you should set the metadata locations for EntityConnectionStringBuilder.Metadata Property: http://msdn.microsoft.com/en-us/library/system.data.entityclient.entityconnectionstringbuilder.metadata.aspx

    In addition, this article is helpful for you I think: http://blogs.teamb.com/craigstuntz/2010/08/13/38628/

    Good day.


    Alexander Sun [MSFT]
    MSDN Community Support | Feedback to us

    Monday, August 6, 2012 3:25 AM
  • Turns out what I was doing was correct.  I had a typo in my code.  Referring to my initial posting, myfirstEntities is the EF entity that was created by VS with a database in a known location.

    var cstring = ConfigurationManager.ConnectionStrings["myfirstEntities"].ConnectionString;

    gets the config string from the app.config file.

    var connectionStringBuilder = new EntityConnectionStringBuilder(cstring);

    uses the cstring to construct the same EntityConnection that EF would.

    connectionStringBuilder.ProviderConnectionString = String.Format("data source={0}",location);

    substitute the actual database location in the EntityConnection Object.

    and

    return new myfirstEntities(connectionStringBuilder.ConnectionString);

    Creates the EntityContext with the revised string.


    Robotuner

    • Marked as answer by Robotuner Tuesday, August 7, 2012 1:22 AM
    Tuesday, August 7, 2012 1:22 AM