locked
Entity Framework - how can I change connection string to be relative? RRS feed

  • Question

  • Hi,

    Can someone confirm how to change the auto-generated connection string for an entity framework application so that it is relative?

    That is so it will work for anyone who downloads and installs the application.  That is, currently the connection string auto-generated for me has an absolute path in it.  See below for an example:

      <add name="Model1Container" connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SQLite;provider connection string='data source=&quot;C:\Documents and Settings\Owner\My Documents\My Dropbox\source\MyProject\MyProjectLibrary\MyProject.sqlite&quot;'" providerName="System.Data.EntityClient" />
    


    I should note that in my solution the UI application project uses a separate business logic layer project where the Entity Framework model is.  I have been copying the auto-generated connection string from this latter project to the UI project to get things to work.

    thanks <script type="text/javascript"></script> <script type="text/javascript"></script>
    Monday, October 4, 2010 2:04 AM

Answers

  • Hi callagga,

     

    You can use |DataDirectory| Substitution String which resolves to a relative path to a mapping and metadata files. It eliminates the need to hard-code the full path which leads to several problems as the full path to the database could be serialized in different places. DataDirectory also makes it easy to share a project and also to deploy an application.

     

    For example, instead of having the following connection string:

    "Data Source= c:\program files\MyApp\Mydb.sdf"

     

    Using DataDirectory, you can have the following connection string:

    “Data Source = |DataDirectory|\Mydb.sdf”

     

    If you choose to copy the database file to your project the connection string will be changed to use the |DataDirectory| relative path.  And also at runtime the |DataDirectory| value is set through the AppDomain.SetData("DataDirectory", objValue) method.

     

    You can get more information about Support for the |DataDirectory| Substitution String from

    http://msdn.microsoft.com/en-us/library/cc716756.aspx

    And you can also try the method in the following link to use a relative database path in the connectionString.
    http://sqlite.phxsoftware.com/forums/p/1639/7063.aspx

     

    Best regards,

    Alex Liang

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by callagga Thursday, October 7, 2010 7:02 AM
    Tuesday, October 5, 2010 7:51 AM

All replies

  • Hi callagga,

     

    You can use |DataDirectory| Substitution String which resolves to a relative path to a mapping and metadata files. It eliminates the need to hard-code the full path which leads to several problems as the full path to the database could be serialized in different places. DataDirectory also makes it easy to share a project and also to deploy an application.

     

    For example, instead of having the following connection string:

    "Data Source= c:\program files\MyApp\Mydb.sdf"

     

    Using DataDirectory, you can have the following connection string:

    “Data Source = |DataDirectory|\Mydb.sdf”

     

    If you choose to copy the database file to your project the connection string will be changed to use the |DataDirectory| relative path.  And also at runtime the |DataDirectory| value is set through the AppDomain.SetData("DataDirectory", objValue) method.

     

    You can get more information about Support for the |DataDirectory| Substitution String from

    http://msdn.microsoft.com/en-us/library/cc716756.aspx

    And you can also try the method in the following link to use a relative database path in the connectionString.
    http://sqlite.phxsoftware.com/forums/p/1639/7063.aspx

     

    Best regards,

    Alex Liang

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by callagga Thursday, October 7, 2010 7:02 AM
    Tuesday, October 5, 2010 7:51 AM
  • thanks - sorry for the late reply - seems I am not getting the alert emails
    Thursday, October 7, 2010 7:02 AM