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

查看:106
本文介绍了连接字符串中的%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>

使用此代码时,总是会引发以下错误:

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%\Folder\Database.sdf从命令提示符它工作正常,如果我删除%APPDATA%和硬编码的路径它工作正常 - 所以它看起来就像%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...

谢谢,

推荐答案

您已经收到了$ code>%APPDATA%或任何其他环境变量不会替换为连接字符串中的相应值。环境变量与操作系统shell相关,它们与命令提示符,因为命令提示符显式解析输入的值并替换环境变量,这不是.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.SpecialF old.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 | 在数据库文件的路径中。)

(Notice the use of |DataDirectory| in the path to the database file.)

然后在应用程序的Main方法中提供 | 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天全站免登陆