实体框架代码第一个Azure连接

时间:2012-02-11 13:33:53

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

我正在使用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;
        }

你能说出发生了什么吗?

6 个答案:

答案 0 :(得分:5)

我遇到了同样的问题。

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

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

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

答案 1 :(得分: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中添加的连接字符串。

英语不好...... =)

答案 2 :(得分:2)

提供者应该是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。

答案 3 :(得分:0)

我试过这样,它可能对你有所帮助。可能是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" />

答案 4 :(得分:0)

试试这个:

  

Data Source = tcp:YOUR-DATABASE-HERE.database.windows.net,1433;   数据库= GolfRounds;               用户ID = YOUR-USERNAME @ YOUR-SERVER;密码= YOUR-密码; Trusted_Connection = FALSE;加密= TRUE;

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

答案 5 :(得分:-1)

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