CStruggle
CStruggle

Reputation: 173

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. Here is what I have done:

  1. Add new DataObject:

    public class Digests : EntityData
    {
        public string Title { get; set; }
        public string ImageURI { get; set; }
    }
    
  2. Create new Controller called DigestsController.cs

  3. Add public DbSet<Digests> DigestItems { get; set; } to my AppContext.cs. This is supposed to create the table that will hold Digests items.

  4. Add context.Set<Digests>().Add(new Digests { Id = "2016", Title = "Test", ImageURI = "someUriHere" }); to my Startup.MobileApp.cs on the Seed under the AppInitializer class. This is supposed to seed a row of data in my Digests table.

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:

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:

enter image description here Also, when I go into SQL Server Management Studio there's nothing! No TodoItems table (which should be there from the template code) and no Digests table.

What am I missing here?

Upvotes: 0

Views: 283

Answers (3)

lindydonna
lindydonna

Reputation: 3875

The question that you referenced (Extending base mobile azure sample (.net backend)) was for Azure Mobile Services. The same code might not work for Azure Mobile Apps, because there have been a lot of changes to the server SDK.

Instead, the easiest way to create a new table controller is to use the tooling in Visual Studio. Install the latest Azure SDK.

Add your new data class, inheriting from EntityData as you have done. Build the project. Then, right-click your project, and select Add -> New Scaffolded Item. Select Azure Mobile Apps Table Controller. 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.

enter image description here

Upvotes: 1

CStruggle
CStruggle

Reputation: 173

Here is what worked for me:

  1. Create DataModels
  2. Create Controllers (Visual Studio automatically prompts the mobile app to create the tables).
  3. Go into Startup.MobileApps.cs and seed if you want to (completely optional).

Upvotes: 0

Jambor - MSFT
Jambor - MSFT

Reputation: 3293

public static 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. I think it is not a good choice. I would suggest you deploy your application to Mobile Service, then try the code below:

        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

Best Regards,

Jambor

Upvotes: 0

Related Questions