Entity Framework 与 Azure SQL 数据库返回 0 行
Entity Framework with Azure SQL Database Returning 0 Rows
我正在 Azure 中设置一个新应用程序,并且我正在使用 Azure SQL 数据库。我正在使用 Entity Framework 从 ASP.NET MVC 应用程序连接到它。
我的设置如下:我有一个包含 2 个项目的解决方案。在我的一个项目中,我有实体模型。这是我设置与 Entity Frameworks 的连接并生成模型的地方(代码优先)。另一个项目是我的 ASP.NET MVC 项目。这是我从第一个项目调用代码以检索数据的地方。我是 运行 来自我的 MVC 项目的以下代码:
using (var db = new MyEntities())
{
var x = db.Table1.FirstOrDefault(); //this is null because nothing in table
}
我的实体项目配置文件如下:
<?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 门户中找到 check/modify 的位置。
我是 Azure 的新手,所以我可能做错了什么,但我似乎找不到我的设置有什么问题。
临时解决方案是更改我的 base("name=DefaultConnection")
以包含整个连接字符串。这允许连接,但显然不理想。
然后,我 运行 在我的 Entity Framework 项目上创建了一个 reverse-engineer,它为我生成了一个正确的连接字符串(包括正确修复上面的 base()
设置) .
我敢肯定大多数人不必走这条路,但我想我会 post 我的解决方案以防万一。
我正在 Azure 中设置一个新应用程序,并且我正在使用 Azure SQL 数据库。我正在使用 Entity Framework 从 ASP.NET MVC 应用程序连接到它。
我的设置如下:我有一个包含 2 个项目的解决方案。在我的一个项目中,我有实体模型。这是我设置与 Entity Frameworks 的连接并生成模型的地方(代码优先)。另一个项目是我的 ASP.NET MVC 项目。这是我从第一个项目调用代码以检索数据的地方。我是 运行 来自我的 MVC 项目的以下代码:
using (var db = new MyEntities())
{
var x = db.Table1.FirstOrDefault(); //this is null because nothing in table
}
我的实体项目配置文件如下:
<?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 门户中找到 check/modify 的位置。
我是 Azure 的新手,所以我可能做错了什么,但我似乎找不到我的设置有什么问题。
临时解决方案是更改我的 base("name=DefaultConnection")
以包含整个连接字符串。这允许连接,但显然不理想。
然后,我 运行 在我的 Entity Framework 项目上创建了一个 reverse-engineer,它为我生成了一个正确的连接字符串(包括正确修复上面的 base()
设置) .
我敢肯定大多数人不必走这条路,但我想我会 post 我的解决方案以防万一。