question

SethIsaacks-9226 avatar image
0 Votes"
SethIsaacks-9226 asked ·

MobileServiceSQLiteStore seed data

Can someone please explain to me how to seed data with MobileServiceSQLiteStore. I was using just a local sqlite db and it was easy. I am not sure how to accomplish this with MobileServiceSQLiteStore. I watched a video with Jamese Montemagno of how to create an azure backend with sqlite. But there was nothing with seeding data and I have yet to find anything.

azure-stack-hubdotnet-sqlite
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

ryanchill avatar image
0 Votes"
ryanchill answered ·

Hi @SethIsaacks-9226,

Mobile App is going through an overhaul, so finding traditional samples will be difficult to come by. Nevertheless, they do have preliminary docs which contain a quick start that my help at least get you started, see https://azure.github.io/azure-mobile-apps/howto/server/dotnet-framework/ on getting ASP.NET Framework backend started. Within that quick start, it has sample code on initializing your server framework. This is where you would want to adapt this area to seed your data through Microsoft.Azure.Mobile.Server.Entity package. Since this package leverages using Entity Framework, you use the same practices for seeding your database through Database.SetIntilizer API, see https://github.com/Azure/azure-mobile-apps-net-server/blob/master/samples/SampleApp/App_Start/Startup.MobileApp.cs.

Seeding data is a customary practice, so I will reach out to the team about including this in their docs as well as https://github.com/Azure/azure-mobile-apps/tree/master/samples. Do note that using SQLite is not recommended for your backend server due to its scaling issues and some restrictions in EF that have be worked around, such as timestamp handling.

Regards,
Ryan



·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

SethIsaacks-9226 avatar image
0 Votes"
SethIsaacks-9226 answered ·

@ryanchill thank you for the reply. I am absolutely struggling with even trying to get data to save in the Azure DB. I can connect to it, I set up everything following the online tutorial....but the data will only save local. I can add data to the azure sql db and my app does not pull it down.

This is the tutorial I followed: https://docs.microsoft.com/en-us/azure/app-service/app-service-web-tutorial-dotnet-sqldatabase#create-a-server

Below is my client side code which connects but data is not being pulled or transferred to the Azure DB. I have spent over a week on this and am beside myself as to what the issue is.

 public async Task Initialize()
 {
     if(client?.SyncContext?.IsInitialized ?? false)
     {
         return;
     }
    
         var appUrl = "https://nfowebapp.azurewebsites.net";
    
     client = new MobileServiceClient(appUrl);
    
     var fileName = "myDB.db";
    
     var store = new MobileServiceSQLiteStore(fileName);
    
     store.DefineTable<AppSettings>();
        
    
     await client.SyncContext.InitializeAsync(store);
    
     appSettingsTable = client.GetSyncTable<AppSettings>();
    
 }
    
 public async Task SyncUserAppSettings()
 {
     try
     {
         if (Xamarin.Essentials.Connectivity.NetworkAccess != Xamarin.Essentials.NetworkAccess.Internet)
         {
             return;
         }
    
         await client.SyncContext.PushAsync();
         await appSettingsTable.PullAsync("allSettings", appSettingsTable.CreateQuery().Where(x => x.UserId == App.UserId));
     }
     catch (Exception ex)
     {
         App.logger.Error(ex.ToString());
     }
 }
    
 public async Task<AppSettings> GetAppSettings()
 {
     await Initialize();
     await SyncUserAppSettings();
    
     AppSettings settings = new AppSettings();
    
     settings = (await appSettingsTable.Where(x => x.UserId == App.UserId).ToListAsync()).FirstOrDefault();
    
     return settings ?? new AppSettings();
 }
· 2 ·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

What are there errors you're getting @SethIsaacks-9226?

0 Votes 0 ·

@ryanchill I am getting slightly confused. You're pointing me to create an Azure Mobile App? I am not trying to create a mobile Azure app, I am trying to connect my app with an azure backend (azure sql database). I have created everything so far in Visual Studio and deployed to my azure portal. But the issue is getting the web app to communicate with azure. I think possibly it has something to do with my routes. The first error I was getting was a 404 The resource cannot be found. Requested URL /tables/Car

So I added a mapping route to RouteConfig.cs in my web app (see below) and now I get an error occurred. Someone told me to use swagger to find out what is going on with the routes.

routes.MapRoute(
name: "Person",
url: "tables/People",
defaults: new { controller = "People", action = "Index" }

I have watch tons of videos and tutorials, all of which our out dated, and this use to be very easy, I dont get why its so hard now.

0 Votes 0 ·
ryanchill avatar image
0 Votes"
ryanchill answered ·

Hi @SethIsaacks-9226,

Your use of Xamarin and the MobileServiceSQLiteStore class indicated that you were creating a standalone app that would pull data from an Azure SQL backend and be stored/cached locally. If the code snippet you've provided is your only route, then your application will only send requests to https://yourapp.azurebsites.net/tables/People and /tables/Car will not be accessible, which could be why you're getting a 404. Update your RouteConfig.cs to

             routes.MapRoute(
                 name: "Default",
                 url: "{controller}/{action}/{id}",
                 defaults: new { controller = "People", action = "Index", id = UrlParameter.Optional }
             );

I also, IMHO, wouldn't use tables as part of the route name. You are wanting to access a resource of People, not a resource of table. I'm assuming you have a PeopleController and CarController classes in your project. The route above instructs the framework to lookup a class called People with a /GET named Index which should return your default dataset from your Azure SQL database, I'm assuming a table named Person.

If you run into errors HTTP 500 errors when accessing your controller, here's a couple of things to check.

  • If running locally, make sure you've added your IP address to the firewall.

  • If running from your deployed app service, ensure that your connection string is correctly configured in your Application Settings blade.

Regards,
Ryan

· 2 ·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@ryanchill what I am looking for is a front to back tutorial of how to create a mobile back end azure sql database and how to connect to it from the mobile app.
I have went through many tutorials and its absolutely frustrating, they are all different. One is Core Web AI, another is a Core Web App......I have yet to find what i need or a good example. I am not sure if I need a Web Api or Web App. I have yet to find that answer.

0 Votes 0 ·
ryanchill avatar image ryanchill SethIsaacks-9226 ·

@SethIsaacks-9226 Azure Mobile Apps is being rescoped as a product offering. For that reason, there isn't a front to back tutorial. https://azure.github.io/azure-mobile-apps/ and the available quick starts is still a suitable place to start. Having said that, I'll see about putting a sample together though.

Regards,
Ryan

0 Votes 0 ·