2015-01-31 65 views
0

我在Azure中設置一個新的應用程序,並使用Azure SQL數據庫。我正在使用實體框架從ASP.NET MVC應用程序連接到它。實體框架與Azure SQL數據庫返回0行

我的設置如下:我有一個包含2個項目的解決方案。在我的一個項目中,我有我的實體模型。這是我建立與Entity Frameworks的連接並生成模型(代碼優先)的地方。另一個項目是我的ASP.NET MVC項目。這是我從第一個項目中調用代碼來檢索數據的地方。

 using (var db = new MyEntities()) 
     { 
      var x = db.Table1.FirstOrDefault(); //this is null because nothing in table 
     } 

爲實體項目我的配置文件如下::我從我的MVC項目運行下面的代碼

<?xml version="1.0" encoding="utf-8"?> 
<configuration> 
    <configSections> 

    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=*********" requirePermission="false" /> 
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --></configSections> 
    <entityFramework> 
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework"> 
     <parameters> 
     <parameter value="mssqllocaldb" /> 
     </parameters> 
    </defaultConnectionFactory> 
    <providers> 
     <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" /> 
    </providers> 
    </entityFramework> 
    <connectionStrings> 
    <add name="MyEntities" connectionString="data source=MY_DATABASE_HERE.windows.net,1433;initial catalog=MyDB;persist security info=True;user id=MY_USERNAME_HERE;password=MY_PASSWORD_HERE;MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient" /> 
    </connectionStrings> 
</configuration> 

我已經刪除了其中的敏感信息,但我已經證實這是正確的 - 我無論如何都沒有收到連接錯誤。我唯一能想到的其他事情是我的用戶沒有配置適當的權限,但我無法找到在Azure門戶中檢查/修改的位置。

我是新來的Azure,所以我可能做錯了什麼,但我似乎無法找到我的設置有什麼問題。

+0

可能是一個愚蠢的問題,但你確定實際上有任何數據在你的分貝? srv mgt studio? – Frans 2015-01-31 20:14:46

+0

另外,您需要tcp:在連接字符串的開頭,但t他其實並沒有看到連接錯誤,表明這不是一個因素。 – Frans 2015-01-31 20:16:06

+0

如果它是一個Azure網站,您應該能夠看到在Azure門戶中使用的實際連接字符串 - 可能值得檢查是否有轉換正在進行。 – Frans 2015-01-31 20:17:41

回答

1

臨時解決方案是將我的base("name=DefaultConnection")更改爲包含整個連接字符串。這允許一個連接,但顯然不理想。

然後,我跑我的實體框架項目進行反向工程,它產生的對我來說是正確的連接字符串(包括正確地將其固定在上面base()設置。

Right click on the Entity Framework project

我敢肯定大多數人不必走這條路,但我想我會發布我的解決方案,以防萬一。