ASP.NET Web API、Entity Framework、Microsoft AzureでホストされるSQLデータベースを使用してREST APIを作成しました。私はCode first migrationを使いました。ここで ASP.NET Web APIサーバーコードは、ローカルで実行するとデータベースに接続しますが、Azureでホストされている場合は接続しません。
はlocalDB上でローカルに、まず私はマイグレーションを作成しnamespace Trivio.Models
{
public partial class User
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public string Password { get; set; }
}
}
DbContextはTrivio.Models.ApplicationDbContext.cs
namespace Trivio.Models
{
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public DbSet<User> UserInformation { get; set; }
public DbSet<Trivia> Trivia { get; set; }
public DbSet<TriviaHistoryModel> TriviaHistoryModel { get; set; }
public ApplicationDbContext()
: base("DefaultConnection", throwIfV1Schema: false)
{
}
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
}
}
の下で定義されたユーザーモデルクラスです。その後、サーバーコードをAzureに更新し、コードの最初の移行を有効にしました。 Azure SQLデータベースに正しいデータが格納されているため、これが機能していることはわかっています。私のデータベースが正しくホストされているかどうかをテストするために、私はconnectionStringを更新してAzure SQLデータベースに接続しました。
ここにWeb.configの接続文字列があります。 Trivio.Controllers.UserController.cs下
<connectionStrings>
<add name="DefaultConnection" connectionString="Server=tcp:trivioserver.database.windows.net,1433;Initial Catalog=Trivio;Persist Security Info=False;User ID={mycorrectusername};Password={mypassword};MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;" providerName="System.Data.SqlClient"/>
</connectionStrings>
私のユーザーAPIコントローラを(私はダミーデータとパスワードとユーザー名を交換した):問題が発生した場所
namespace Trivio.Controllers
{
[RoutePrefix("Trivia")]
public class UserController : ApiController
{
private ApplicationDbContext _context;
public UserController()
{
_context = new ApplicationDbContext();
}
//------------Routes---------------------------------------------
//Return List of Users
[HttpGet]
[Route("Users")]
public IEnumerable<User> GetUsers()
{
return _context.UserInformation.ToList();
}
}
}
がここにあります。ローカルサーバー上でコードを実行し、localserver/Trivia/UsersへのGET呼び出しを行って、Azure SQLデータベースから正しいユーザーリストを返します。
しかし、Azureに公開した後、全く同じコードがユーザーの空のリストを返します。私はhttp:// {myappname} .azurewebsites.net/Trivia/UsersへのGET呼び出しを行いました。
<ArrayOfUser xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/Trivio.Models"/>
私はhtmlコードがロードされるため、コードが正しくホストされている知っている 。
Trivio.Azure
下のマイServiceConfigurationCloudファイル<?xml version="1.0" encoding="utf-8"?>
<ServiceConfiguration serviceName="Trivio.Azure" xmlns="http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceConfiguration" osFamily="4" osVersion="*" schemaVersion="2015-04.2.6">
<Role name="Trivio">
<Instances count="1" />
<ConfigurationSettings>
<Setting name="Microsoft.WindowsAzure.Plugins.Diagnostics.
" value="UseDevelopmentStorage=true" />
</ConfigurationSettings>
</Role>
</ServiceConfiguration>
私は手動でそれと同じデータベースに戻り、正しい情報に接続されたローカルサーバーのコードをチェックするため、データベースには、空でないことを知っています。
(紺碧のUI +テーブルストレージを経由してそれを設定する方法をtheresの:私はこの問題を解決/識別するための簡単な方法は、リモートデバッガを使用し、間違っているものを調べだと思います)、結果が0に戻っていないことがわかりますが、実際にはエラーが発生しており、エラーは一般公開されていません。 –