MobileServiceSQLiteStore seed data

Seth Isaacks 11 Reputation points
2021-03-09T20:02:54.457+00:00

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.

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
13,277 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Ryan Hill 26,946 Reputation points Microsoft Employee
    2021-03-12T16:42:12.35+00:00

    Hi @Seth Isaacks ,

    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

    0 comments No comments

  2. Seth Isaacks 11 Reputation points
    2021-03-12T17:22:09.6+00:00

    @Ryan Hill 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://learn.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();  
    }  
    

  3. Ryan Hill 26,946 Reputation points Microsoft Employee
    2021-03-15T16:44:54.257+00:00

    Hi @Seth Isaacks ,

    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