简体   繁体   中英

Entity Framework with Azure SQL Database Returning 0 Rows

I'm setting up a new application in Azure and I'm using an Azure SQL database. I'm using Entity Framework to connect to it from an ASP.NET MVC application.

My setup is as follows: I have a solution with 2 projects in it. In my one project, I have my Entity Models. This is where I've set up my connection to Entity Frameworks and the models were generated (code first). The other project is my ASP.NET MVC project. This is where I'm calling code from the first project to retrieve data. I'm running the following code from my MVC project:

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

My config file for the Entity project is as follows:

<?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>

I've removed the sensitive information from it, but I've verified that's right - I don't get a connection error anyway. The only other thing I could think of is that my user is not configured with proper permissions, but I can't find where to check/modify that in the Azure portal.

I'm new to Azure, so I'm probably doing something wrong, but I can't seem to find what's wrong with my setup.

The temporary solution for this was to change my base("name=DefaultConnection") to instead contain the entire connection string. This allowed a connection, but was obviously not ideal.

Then, I ran a reverse-engineer on my Entity Framework project and it generated a proper connection string for me (including properly fixing the above base() setting with it.

右键单击Entity Framework项目

I'm sure most people won't have to go this route, but I figured I'd post my solution just in case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM