2017-01-29 6 views
0

私はAzure Mobile Appを作成し、生成されたバックエンドとモバイルアプリをダウンロードしました。私がそれを始めると、db内の2つのtodo項目が正常に取得されます。ただし、以下のようにユーザーモデルを追加するとすぐに、モバイルアプリでToDo項目が取得されなくなります。私は何が紛争を引き起こしているのか分かりません。私はユーザモデルに&の参照を削除した場合、私はDBcontextからこの行をコメントアウトすると、それが再び動作する C#Azureモバイルアプリ - 新しいモデルを追加できない

  • 、私は再びモバイルアプリのTODOアイテムを取得することができます。

    //public DbSet<User> Users { get; set; } 
    

    スタートアップ:

    public partial class Startup 
        { 
         public static void ConfigureMobileApp(IAppBuilder app) 
         { 
          HttpConfiguration config = new HttpConfiguration(); 
    
          //For more information on Web API tracing, see http://go.microsoft.com/fwlink/?LinkId=620686 
          config.EnableSystemDiagnosticsTracing(); 
    
          new MobileAppConfiguration() 
           .UseDefaultConfiguration() 
           .ApplyTo(config); 
    
          // Map routes by attribute 
          config.MapHttpAttributeRoutes(); 
    
          // Use Entity Framework Code First to create database tables based on your DbContext 
          Database.SetInitializer(new AuthApp231Initializer()); 
    
          // To prevent Entity Framework from modifying your database schema, use a null database initializer 
          // Database.SetInitializer<AuthApp231Context>(null); 
    
          MobileAppSettingsDictionary settings = config.GetMobileAppSettingsProvider().GetMobileAppSettings(); 
    
          if (string.IsNullOrEmpty(settings.HostName)) 
          { 
           // This middleware is intended to be used locally for debugging. By default, HostName will 
           // only have a value when running in an App Service application. 
           app.UseAppServiceAuthentication(new AppServiceAuthenticationOptions 
           { 
            SigningKey = ConfigurationManager.AppSettings["SigningKey"], 
            ValidAudiences = new[] { ConfigurationManager.AppSettings["ValidAudience"] }, 
            ValidIssuers = new[] { ConfigurationManager.AppSettings["ValidIssuer"] }, 
            TokenHandler = config.GetAppServiceTokenHandler() 
           }); 
          } 
          app.UseWebApi(config); 
         } 
        } 
    
        public class AuthApp231Initializer : CreateDatabaseIfNotExists<AuthApp231Context> 
        { 
         protected override void Seed(AuthApp231Context context) 
         { 
          List<TodoItem> todoItems = new List<TodoItem> 
          { 
           new TodoItem { Id = Guid.NewGuid().ToString(), Text = "First item", Complete = false }, 
           new TodoItem { Id = Guid.NewGuid().ToString(), Text = "Second item", Complete = false }, 
          }; 
    
          foreach (TodoItem todoItem in todoItems) 
          { 
           context.Set<TodoItem>().Add(todoItem); 
          } 
    
          /*List<User> users = new List<User> 
          { 
           new User { Id = Guid.NewGuid().ToString(), Username = "adrian", Password = "supersecret" } 
          }; 
    
          foreach (User user in users) 
          { 
           context.Set<User>().Add(user); 
          }*/ 
    
          base.Seed(context); 
         } 
        } 
    

    ダットaObjects:

    public class User :EntityData 
        { 
    
         public string Username { get; set; } 
    
         public string Password { get; set; } 
        } 
    

    コンテキスト:

    public class AuthApp231Context : DbContext 
        { 
         // You can add custom code to this file. Changes will not be overwritten. 
         // 
         // If you want Entity Framework to alter your database 
         // automatically whenever you change your model schema, please use data migrations. 
         // For more information refer to the documentation: 
         // http://msdn.microsoft.com/en-us/data/jj591621.aspx 
    
         private const string connectionStringName = "Name=MS_TableConnectionString"; 
    
         public AuthApp231Context() : base(connectionStringName) 
         { 
         } 
    
         public DbSet<TodoItem> TodoItems { get; set; } 
         public DbSet<User> Users { get; set; } 
    
         protected override void OnModelCreating(DbModelBuilder modelBuilder) 
         { 
          modelBuilder.Conventions.Add(
           new AttributeToColumnAnnotationConvention<TableColumnAttribute, string>(
            "ServiceTableColumn", (property, attributes) => attributes.Single().ColumnType.ToString())); 
         } 
        } 
    

答えて

0

私が知られているように、サンプルモバイルアプリのバックエンドのプロジェクトのみTodoItemモデルが含まれている、あなたのプロジェクトを実行すると、Database.SetInitializer(new AuthApp231Initializer())がトリガーと作成されるだろうデータベースが存在しない場合はTodoItemテーブル。データベースの初期化後に別のデータモデルを追加する場合は、データベースを移行する必要があります。

注:カスタマイジングの移行を追加し、最新のデータベースを持ってUpdate-Databaseを使用するAdd-Migrationを活用できます。また、MigrateDatabaseToLatestVersionイニシャライザを活用してデータベースを自動的にアップグレードすることもできます。

EFコードファーストマイグレーションを有効にする方法の詳細については、この公式tutorialを参照してください。

0

既存のプロジェクトにモデルを追加するには、次の操作を行う必要があります

  1. 新しいDTOためTableControllerを追加し、コンテキスト
  2. にDbSetを追加
  3. データ転送オブジェクトを追加します。
  4. を設定し、ターゲットデータベースに対してマイグレーションを実行する、またはそうでなければデータベース内の適切なテーブルを作成

あなたは#1と#2を終えたと説明しました。あなたは#3か#4をしたことを知らされていません。詳細については

http://aka.ms/zumobookで私の本をチェックアウト - 特に章返信3.

+0

感謝を。私は実際にあなたの本(第2章)に従って認証を実装しました。私はすべてのステップを踏んだが、それを動作させることができなかった。ユーザーモデル用。 DataAnnotationsはもはや有効ではありません。そのため、私はユーザーにEntityDataを使用しました。 –

+0

EntityDataは、Entity Frameworkを使用するITableDataの単なる実装です。 DataAnnotationsはEntity Frameworkから提供されます。エンティティフレームワークの.NETCore版にアップグレードした場合(最も確実に行うべきではない)、これが可能性の高い結果です。 –

関連する問題