简体   繁体   English

种子表.NET后端Azure移动应用程序错误

[英]Seed table .NET backend Azure Mobile Apps error

I want to add a DataObject of my own to Azure Mobile Apps on top of the TodoItem that is supplied in the template. 我想将自己的DataObject添加到模板中提供的TodoItem之上的Azure移动应用程序。 Here is what I have done: 这是我所做的:

  1. Add new DataObject : 添加新的DataObject

     public class Digests : EntityData { public string Title { get; set; } public string ImageURI { get; set; } } 
  2. Create new Controller called DigestsController.cs 创建名为DigestsController.cs新控制器

  3. Add public DbSet<Digests> DigestItems { get; set; } 添加public DbSet<Digests> DigestItems { get; set; } public DbSet<Digests> DigestItems { get; set; } public DbSet<Digests> DigestItems { get; set; } to my AppContext.cs . public DbSet<Digests> DigestItems { get; set; }到我的AppContext.cs This is supposed to create the table that will hold Digests items. 这应该创建将包含Digests项目的表。

  4. Add context.Set<Digests>().Add(new Digests { Id = "2016", Title = "Test", ImageURI = "someUriHere" }); 添加context.Set<Digests>().Add(new Digests { Id = "2016", Title = "Test", ImageURI = "someUriHere" }); to my Startup.MobileApp.cs on the Seed under the AppInitializer class. 到我在AppInitializer类下的Seed上的Startup.MobileApp.cs This is supposed to seed a row of data in my Digests table. 这应该在我的Digests表中播种一行数据。

According to this question and the way he solved it, this should work! 根据这个问题和他解决问题的方式,这应该可行! But whenever I try to pull down data from my client app: 但是每当我尝试从客户端应用程序中提取数据时:

public static MobileServiceClient MobileService = new MobileServiceClient("http://app.azurewebsites.net/");
IMobileServiceTable<Digests> Digests = App.MobileService.GetTable<Digests>();
Weeks = await Digests.ToListAsync();

Or pull down data with the template app that I can download from Azure Portal: 或使用可以从Azure门户下载的模板应用程序提取数据:

private MobileServiceCollection<TodoItem, TodoItem> items;
private IMobileServiceTable<TodoItem> todoTable = App.MobileService.GetTable<TodoItem>();
items = await todoTable.Where(todoItem => todoItem.Complete == false).ToCollectionAsync();

I get this: 我得到这个:

在此处输入图片说明 Also, when I go into SQL Server Management Studio there's nothing! 另外,当我进入SQL Server Management Studio时,什么都没有! No TodoItems table (which should be there from the template code) and no Digests table. 没有TodoItems表(应该在模板代码中存在该表),也没有Digests表。

What am I missing here? 我在这里想念什么?

The question that you referenced ( Extending base mobile azure sample (.net backend) ) was for Azure Mobile Services. 您引用的问题( 扩展基本的移动天蓝色样本(.net后端) )是针对Azure移动服务的。 The same code might not work for Azure Mobile Apps, because there have been a lot of changes to the server SDK. 相同的代码可能不适用于Azure移动应用程序,因为服务器SDK进行了大量更改。

Instead, the easiest way to create a new table controller is to use the tooling in Visual Studio. 相反,创建新表控制器的最简单方法是使用Visual Studio中的工具。 Install the latest Azure SDK . 安装最新的Azure SDK

Add your new data class, inheriting from EntityData as you have done. 添加新数据类,并从EntityData继承。 Build the project. 生成项目。 Then, right-click your project, and select Add -> New Scaffolded Item . 然后,右键单击您的项目,然后选择添加->新建脚手架项目 Select Azure Mobile Apps Table Controller. 选择“ Azure移动应用程序表控制器”。 See screen shot below. 请参阅下面的屏幕截图。

Note that publishing your project does not create the database table, since Entity Framework Code First creates the database once it is accessed for the first time. 请注意,发布项目不会创建数据库表,因为实体框架代码优先会在第一次访问数据库时创建该数据库。 See The server database is not created in the Mobile Apps Wiki. 请参阅在Mobile Apps Wiki中创建服务器数据库

在此处输入图片说明

public static MobileServiceClient MobileService = new MobileServiceClient(" http://app.azurewebsites.net/ "); 公共静态MobileServiceClient MobileService = new MobileServiceClient(“ http://app.azurewebsites.net/ ”);

From the code you provided. 根据您提供的代码。 Based on my understanding, you deployed your mobile application to Azure Web app. 根据我的理解,您已将移动应用程序部署到Azure Web应用程序。 I think it is not a good choice. 我认为这不是一个好选择。 I would suggest you deploy your application to Mobile Service, then try the code below: 我建议您将应用程序部署到Mobile Service,然后尝试以下代码:

        public static MobileServiceClient MobileService = new MobileServiceClient(
            "https://<mobile service name>.azure-mobile.net/",
            "<mobile service key>"  //we could find this key in Azure portal Mobile service -> management key
        );

If your data model changes, please refer to https://azure.microsoft.com/en-us/documentation/articles/mobile-services-dotnet-backend-how-to-use-code-first-migrations/#seeding fore more dtails 如果您的数据模型发生变化,请参考https://azure.microsoft.com/zh-cn/documentation/articles/mobile-services-dotnet-backend-how-to-use-code-first-migrations/#seeding前更多的尾巴

Best Regards, 最好的祝福,

Jambor 詹博

Here is what worked for me: 这对我有用:

  1. Create DataModels 创建数据模型
  2. Create Controllers (Visual Studio automatically prompts the mobile app to create the tables). 创建控制器(Visual Studio自动提示移动应用创建表)。
  3. Go into Startup.MobileApps.cs and seed if you want to (completely optional). 如果需要,进入Startup.MobileApps.cs并添加种子(完全可选)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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