在连接字符串%APPDATA%不代替实际文件夹? [英] %APPDATA% in connection string is not substituted for the actual folder?

查看:90
本文介绍了在连接字符串%APPDATA%不代替实际文件夹?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用WPF和实体框架我有一个看起来像下面一个app.config:

When using WPF and entity-framework I have an APP.CONFIG that looks like the 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>

在使用本code,它总是引发以下错误:

When using this code it always throws the following error:

System.Data.EntityException: The underlying provider failed on Open. ---> System.Data.SqlServerCe.SqlCeException: The path is not valid. Check the directory for the database. [ Path = %APPDATA%\Folder\Database.sdf ]

当我运行路径%APPDATA%\\文件夹\\ Database.sdf从命令提示符它工作正常,如果我删除%APPDATA%和硬code的路径,它工作正常 - 所以它看起来只是喜欢%APPDATA%是不被取代​​实际的文件夹...

When I run the path "%APPDATA%\Folder\Database.sdf" from the command prompt it works fine, and if I remove "%APPDATA% and hardcode the path it works fine - so it looks simply like the %APPDATA% is just not being substituted for the actual folder...

谢谢,

推荐答案

正如你已经reallized,%APPDATA%或任何其他environtment变量没有各自的值替换在连接字符串。环境varialbes是东西相关的操作系统的外壳。他们命令提示符下工作,因为在命令提示符下明确地解析输入的值并替换环境变量。这不是一件.NET Framwork通常执行。

As you already reallized, %APPDATA% or any other environtment variables are not replaced with their respective value in connection strings. Environment varialbes are something related to the operating system shell. They work in command prompt because the command prompt explicitly parses the values entered and substitutes environment variables. That's not something that .NET Framwork usually performs.

为了更好地实现这一点,你必须手动为%APPDATA%(使用 Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)值 Environment.GetEnvironmentVariable(APPDATA))。有两个选项:

To achive this, you have to manually provide the value for %APPDATA% (using Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) or Environment.GetEnvironmentVariable("APPDATA")). There are two options:


  1. 更​​改连接字符串,并使用 | 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>

(注意使用的| DataDirectory目录| 路径的数据库文件)

然后提供价值 | DataDirectory目录| 在应用程序的主要方法:

Then provide the value for |DataDirectory| in your application's Main method:

AppDomain.CurrentDomain.SetData("DataDirectory",
    Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData));

参阅了解更多信息这个MSDN页面。

手动为您提供ObjectContext类的连接字符串。这样您就可以解析和更改连接字符串:

Manually provide the connection string for your ObjectContext class. This way you can parse and change the connection string:

public static string GetConnectionString()
{
    var conStr = System.Configuration.ConfigurationManager.ConnectionStrings["DatabaseEntities"].ConnectionString;
    return conStr.Replace("%APPDATA%",
        Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData));
}

和更高版本:

var db = new DatabaseEntities(GetConnectionString());

或子你的ObjectContext 类,并一直使用新的连接字符串:

Or subclass you ObjectContext class and always use the 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 the new class anywhere.

这篇关于在连接字符串%APPDATA%不代替实际文件夹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆