c# - %APPDATA% in connection string is not substituted for the actual folder? -


when using wpf , entity-framework have app.config looks following:

<?xml version="1.0" encoding="utf-8"?> <configuration>   <connectionstrings>      <add name="databaseentities" connectionstring="metadata=res://*/model.csdl|res://*/model.ssdl|res://*/model.msl;provider=system.data.sqlserverce.4.0;provider connection string=&quot;data source=%appdata%\folder\database.sdf&quot;" providername="system.data.entityclient" />   </connectionstrings> </configuration> 

when using code throws following error:

system.data.entityexception: underlying provider failed on open. ---> system.data.sqlserverce.sqlceexception: path not valid. check directory database. [ path = %appdata%\folder\database.sdf ] 

when run path "%appdata%\folder\database.sdf" command prompt works fine, , if remove "%appdata% , hardcode path works fine - looks %appdata% not being substituted actual folder...

thanks,

as reallized, %appdata% or other environtment variables not replaced respective value in connection strings. environment varialbes related operating system shell. work in command prompt because command prompt explicitly parses values entered , substitutes environment variables. that's not .net framwork performs.

to achive this, have manually provide value %appdata% (using environment.getfolderpath(environment.specialfolder.applicationdata) or environment.getenvironmentvariable("appdata")). there 2 options:

  1. change connection string , use |datadirectory|:

    <connectionstrings>   <add name="databaseentities" connectionstring="metadata=res://*/model.csdl|res://*/model.ssdl|res://*/model.msl;provider=system.data.sqlserverce.4.0;provider connection string=&quot;data source=|datadirectory|\database.sdf&quot;" providername="system.data.entityclient" /> </connectionstrings> 

    (notice use of |datadirectory| in path database file.)

    then provide value |datadirectory| in application's main method:

    appdomain.currentdomain.setdata("datadirectory",     environment.getfolderpath(environment.specialfolder.applicationdata)); 

    refer this msdn page more information.

  2. manually provide connection string objectcontext class. way can parse , change connection string:

    public static string getconnectionstring() {     var constr = system.configuration.configurationmanager.connectionstrings["databaseentities"].connectionstring;     return constr.replace("%appdata%",         environment.getfolderpath(environment.specialfolder.applicationdata)); } 

    and later:

    var db = new databaseentities(getconnectionstring()); 

    or subclass objectcontext class , use new connection string:

    public class mydatabaseentities : databaseentities {     public mydatabaseentities()         : base(getconnectionstring())     {     }      public static string getconnectionstring()     {         var constr = system.configuration.configurationmanager.connectionstrings["databaseentities"].connectionstring;         return constr.replace("%appdata%",             environment.getfolderpath(environment.specialfolder.applicationdata));     } } 

    and use new class anywhere.


Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

Function that returns a formatted array in VBA -