如何使用实体框架的时候,包括在连接字符串密码符号? [英] How to include ampersand in connection string password when using Entity Framework?

查看:120
本文介绍了如何使用实体框架的时候,包括在连接字符串密码符号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用实体框架4一个简单的应用程序,并希望我的烤连接凭据到下面的连接字符串:

I'm using Entity Framework 4 for a simple app and would like to bake my connection credentials into the following connection string:

<connectionStrings>
    <add name="MyEntities"    
         connectionString="metadata=res://*/MyDataModel.csdl|res://*/MyDataModel.ssdl|res://*/MyDataModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=localhost\DEV;Initial Catalog=MyDB;UserId=myUser;Password=jack&jill;MultipleActiveResultSets=True&quot;" 
         providerName="System.Data.EntityClient" />
</connectionStrings>

不过,密码(我不能改变)包含一个符号。 ASP.NET抛出:
配置错误:在解析实体名称出现错误。行XX,位置YYY。

如果我用&放替换密码符号;放大器; ,我收到了的SQLException:登录失败,用户MYUSER通常这一招的作品,但我猜的东西是失败的,因为这在技术上是一个连接字符串里面的连接字符串。

If I replace the ampersand in the password with &amp;, I get a SqlException: Login failed for user 'myUser'. Usually this trick works, but I'm guessing that something is failing because this is technically a connection string inside a connection string.

我应该怎么做吗?我的大多数课程包括code,如:

What should I do here? Most of my classes include code like:

using (var context = new MyEntities()) {
   // do work
}


更新:事实证明,我使用的凭据是域帐户,所以我真正需要的是集成安全性= TRUE 中连接字符串,而不是一个密码。


Update: It turns out that the credentials I am using are a domain account, so what I really need is Integrated Security=True in the connection string rather than a password.

编码符号作为公认的答案表示的的做工精细,虽然我没有测试过。

Encoding the ampersand as indicated in the accepted answer should work fine, though I haven't tested it.

推荐答案

您需要使用转义序列,就像您对任何XML文档,这是所有的.config文件。

You'll need to use escape sequences like you would for any XML document, which is all the .config files are.


  • &符号=安培; = &放大器;放大器;

  • 大于=> = &放大器; GT;

  • 小于=&LT; = &放大器; LT;

  • =撇号'= &放大器;者;

  • 报价== &放大器; QUOT;

  • Ampersand = & = &amp;
  • Greater Than = > = &gt;
  • Less Than = < = &lt;
  • Apostrophe = ' = &apos;
  • Quote = " = &quot;

您也可以使用 CDATA 标记,以便您可以使用这些非法字符

You can also use the CDATA tag so that you can use these illegal characters

&LT;![CDATA [结束]&GT;

<connectionStrings>
    <add name="MyEntities" connectionString="
        metadata=res://*/MyDataModel.csdl|res://*/MyDataModel.ssdl|res://*/MyDataModel.msl;
        provider=System.Data.SqlClient;
        provider connection string=&quot;
        Data Source=localhost\DEV;
        Initial Catalog=MyDB;UserId=myUser;
        Password=<![CDATA[jack&jill]]>;
        MultipleActiveResultSets=True&quot;" 
        providerName="System.Data.EntityClient" />
</connectionStrings>

这篇关于如何使用实体框架的时候,包括在连接字符串密码符号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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