【问题标题】:Entity Framework Code First Azure connection实体框架代码优先 Azure 连接
【发布时间】:2012-03-03 16:19:20
【问题描述】:

我正在使用 Entity Framework Code First 4.3 + Azure 并且无法连接到数据库。我得到的错误如下(在第一个查询中):

Keyword not supported: 'server'.

我在我的 Web.config 中设置了以下连接

<configSections>
    type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=4.3.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
  </configSections>

  <connectionStrings>
    <add name="TestDBContext"
         connectionString="Server=tcp:[SUBSCR].database.windows.net,1433;Database=[MyDB];User ID=[user];Password=[pass];Trusted_Connection=False;Encrypt=True;PersistSecurityInfo=True"
         providerName="System.Data.EntityClient" />
  </connectionStrings>

我的 DbContext 实现类使用连接字符串的名称:

public class MyContext : DbContext, IMyContext
    {
        public MyContext()
            : base("TestDBContext")
        {
            Configuration.LazyLoadingEnabled = true;
            Configuration.ProxyCreationEnabled = true;
        }

你能告诉我发生了什么吗?

【问题讨论】:

    标签: entity-framework azure ef-code-first connection-string code-first


    【解决方案1】:

    我也遇到了同样的问题。

    您缺少 Entity Framework 所需的连接字符串中的所有元数据。 SQL Azure提供的连接字符串需要插入到EF的连接字符串的provider connection string参数中。

    &lt;add name="MyConnectionString" connectionString="metadata=res://*/Model.Model.csdl|res://*/Model.Model.ssdl|res://*/Model.Model.msl;provider=System.Data.SqlClient;provider connection string=&amp;quot;[PUT SQL AZURE CONN STRING HERE]&amp;quot;" providerName="System.Data.EntityClient" /&gt;

    您需要使用自己项目中的元数据。我从现有数据库生成的 EF 项目中提取了该元数据。

    【讨论】:

    • 我首先使用的是 EF 代码 + DbContext(我没有从数据库中生成我的数据模型),所以我在开发时没有任何可用的元数据。
    • @Sese 如果您使用 Code First,那么您的连接字符串提供程序应该是 System.Data.SqlClient 而不是 System.Data.EntityClient
    【解决方案2】:

    我遇到了同样的问题。我解决了,把这个连接字符串放在 web.config 中:

    <add name="eManagerTurModelConnection" connectionString="metadata=res://*/ORM.eManagerFinanceModel.csdl|res://*/ORM.eManagerFinanceModel.ssdl|res://*/ORM.eManagerFinanceModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=<server>.database.windows.net;Initial Catalog=eManagerTur;Integrated Security=False;User ID=<user>;Password=<Password>;Connect Timeout=15;Encrypt=False;TrustServerCertificate=False;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
    

    在我删除我网站的连接字符串后,它工作了,因为它没有获得我在 web.config 中添加的连接字符串。

    英语不好... =)

    【讨论】:

      【解决方案3】:

      提供者应该是 providerName="System.Data.SqlClient"

      我从 VS 连接到 Azure,然后查看属性并设置我的连接字符串和提供程序名称。

       <add name="context" connectionString="Data Source=myServer,myPort;Initial Catalog=myDBName;Persist Security Info=True;User ID=myUserName;Password=myPassword;" providerName="System.Data.SqlClient"/>
      

      然后我可以毫无问题地运行 update-database。

      【讨论】:

        【解决方案4】:

        我试过这样,它可能会帮助你。可能是 1433 出了问题,是端口号吗?要不然是啥? .试试这样。

        查看此链接Windows Azure with Sql

        <add name="dbContext" connectionString="Server=tcp:xxxxxxxx.database.windows.net;Database=xxxxxxxx;User ID=xxxxxxx@xxxxxxxxx;Password=xxxxxxxxxx;Trusted_Connection=False;Encrypt=True;" providerName="System.Data.EntityClient" />
        

        【讨论】:

        • 试过了,它不起作用...1433是sql server的默认端口,我从我的Azure订阅/数据库中复制粘贴了连接字符串。
        【解决方案5】:

        试试这个:

        数据源=tcp:YOUR-DATABASE-HERE.database.windows.net,1433; 数据库=GolfRounds; 用户 ID=您的用户名@您的服务器;密码=您的密码; Trusted_Connection=False;加密=真;

        http://msdn.microsoft.com/en-us/library/windowsazure/ff951633.aspx 上还有一篇 MSDN 文章可能会有所帮助。

        【讨论】:

        • 不支持关键字:“数据源”。
        【解决方案6】:

        我遇到了类似的问题,我无法访问元数据,在这种情况下,您需要使用 System.Data.SqlClient 作为提供程序。您还需要将 MultipleActiveResultSets=True 添加到您的连接字符串

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-04-09
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多