【问题标题】:Seed table .NET backend Azure Mobile Apps error种子表 .NET 后端 Azure 移动应用程序错误
【发布时间】:2016-07-10 16:57:27
【问题描述】:

我想在模板中提供的 TodoItem 之上将我自己的 DataObject 添加到 Azure 移动应用程序。这是我所做的:

  1. 添加新的DataObject

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

  3. public DbSet<Digests> DigestItems { get; set; } 添加到我的AppContext.cs。这应该创建将包含 Digests 项目的表。

  4. SeedAppInitializer 类下将context.Set<Digests>().Add(new Digests { Id = "2016", Title = "Test", ImageURI = "someUriHere" }); 添加到我的Startup.MobileApp.cs。这应该在我的Digests 表中播种一行数据。

根据this question 和他解决它的方式,这应该可以!但是每当我尝试从我的客户端应用程序中提取数据时:

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

或者使用我可以从 Azure 门户下载的模板应用程序拉取数据:

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

我明白了:

此外,当我进入 SQL Server Management Studio 时,什么也没有!没有TodoItems 表(模板代码中应该有),也没有Digests 表。

我在这里错过了什么?

【问题讨论】:

    标签: .net azure azure-sql-database azure-mobile-services


    【解决方案1】:

    您提到的问题 (Extending base mobile azure sample (.net backend)) 是针对 Azure 移动服务的。相同的代码可能不适用于 Azure 移动应用,因为服务器 SDK 发生了很多变化。

    相反,创建新表控制器的最简单方法是使用 Visual Studio 中的工具。安装最新的Azure SDK

    添加您的新数据类,继承自 EntityData,就像您所做的那样。构建项目。然后,右键单击您的项目,然后选择Add -> New Scaffolded Item。选择 Azure 移动应用表控制器。请参见下面的屏幕截图。

    请注意,发布项目不会创建数据库表,因为 Entity Framework Code First 会在第一次访问时创建数据库。请参阅移动应用 Wiki 中的 The server database is not created

    【讨论】:

    • 我实际上发现移动服务和移动应用程序之间的引用发生了变化,所以即使我的代码是正确的,我也会因为没有安装正确的包而出错。非常感谢您的帮助!
    • 没错,你需要匹配正确的客户端和服务器包。见azure.microsoft.com/en-us/documentation/articles/…
    【解决方案2】:

    public static MobileServiceClient MobileService = new MobileServiceClient("http://app.azurewebsites.net/");

    根据您提供的代码。根据我的理解,您已将移动应用程序部署到 Azure Web 应用程序。我认为这不是一个好的选择。我建议您将应用程序部署到 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
            );
    

    如果您的数据模型发生变化,请参考https://azure.microsoft.com/en-us/documentation/articles/mobile-services-dotnet-backend-how-to-use-code-first-migrations/#seeding了解更多详情

    最好的问候,

    贾博尔

    【讨论】:

    • Another Msft referent recommended me to use Mobile Apps b/c 移动服务正在逐步淘汰。有没有办法用移动应用解决这个问题?为什么我需要切换回来?
    • 我也曾无数次重新访问 Azure 链接进行播种。我正在寻找的答案不存在。
    • @ananhalz 是的,我来自 Azure 移动产品团队,您绝对应该切换到移动应用。移动服务已弃用,不应用于新应用。
    • 别担心!一旦你建议我切换,我就一直在使用移动应用程序。这是一个学习过程,但同时也非常棒,谢谢!
    【解决方案3】:

    这对我有用:

    1. 创建数据模型
    2. 创建控制器(Visual Studio 会自动提示移动应用创建表)。
    3. 如果需要,请进入 Startup.MobileApps.cs 并播种(完全可选)。

    【讨论】:

      猜你喜欢
      • 2014-08-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多