Azure Mobile Services Starter Issues - Database - sql-server

I'm trying to create a simple backend for a html/JavaScript app. I started off by creating a new visual Studio Project using the azure mobile Service template. I created a simple DataObject class called 'ProjectItem' which is looking like this:
public class ProjectItem : Microsoft.WindowsAzure.Mobile.Service.EntityData
{
public string Title { get; set; }
public string Description { get; set; }
}
After that I added a table Controller and there was no need to touch any Code in that class.
In the web api config I added this peace of Code:
List<ProjectItem> projectItems = new List<ProjectItem>
{
new ProjectItem {Id = Guid.NewGuid().ToString(), Title = "First Project", Description = "My First Project Description" },
new ProjectItem {Id = Guid.NewGuid().ToString(), Title = "Second Project", Description = "My Second Project Description" }
};
foreach (ProjectItem projectItem in projectItems)
{
context.Set<ProjectItem>().Add(projectItem);
}
I debugged the Service on my localhost and it was working just fine.
Now I wanted to host it on azure and there the Problems started for me.
Here the stepps I did using azure:
I created an empty database on the new Portal
In visual Studio I clicked publish (my solution) and created a new mobile Service selecting the empty database - I didn't touch the rest of the Settings. The mobile Service is now "up and running".
I only have worked with sqlite since now so I opened the database in visual Studio and wanted to look at the test items I created (the 2 Project Items).
But there is no ProjectItem table and no ToDoItem table either!
How can this happen?
What did I do wrong?
Why is it creating the tables running on localhost but when
Publishing on azure it isn't?

I believe that it is not that simple as to create the object model and it will create the same table in the backend (SQL Azure to be clear, or Azure Storage). You need to create it using Azure dashboard, or do the custom API and do as described here. You may do the same in the Visual Studio, i think, or from the SQL Server Management Studio connected to the SQL Azure db.
For Mobile Services, however, if you set the dynamic schema on the dashboard, it will be able to create new columns when they are in the object, but it is not recommended in the production.
So, you did not anything wrong, it looks that the creation of the table programmatically is just not a supported way (only by dashboard or any type of the explorer like SSMS or VS). Anyway (by the way), they will be not in the System Tables branch.

Related

Connection String not found Azure Data Studio to VS2022 - Mac

I am trying to setup my uni work (which is windows based) onto my mac. I have a docker container running with AzureSQLEdge, which I have connected to my Azure Data Studio and can run queries no problem. However in VS2022 trying to set up the connection to the database in Azure Data Studio.
At the moment I am just trying to get some IDs printed to the page (we are using #razor)
#using WebMatrix.Data
#{
ViewBag.Title = "About Us";
var db = Database.Open("AmazonOrders");
var select = "SELECT * FROM Customers";
var data = db.Query(select);
}
<h2 class="text-center">#ViewBag.Title</h2>
<h3>#ViewBag.Message</h3>
<p>Use this area to provide additional information</p>
#foreach (var row in data)
{
#row.customerID
}
My connection string in appsettings.json is:
"AllowedHosts": "*",
"ConnectionStrings": {
"AmazonOrders": "Server=tcp:127.0.0.1,1433;Database=AmazonOrders;User=sa;Password=SQLserver123!;"
}
However I am getting this error message when I load the page:
InvalidOperationException: Connection string "AmazonOrders" was not found.
Setting up the connection string is all the windows users have to do, they have a local version of the database in their project solution. Any help is appreciated, the resources online are more about creating db contexts which I would prefer to avoid if possible, and just connect directly to my database on docker/azure data studio.
edit
Here is the full appsettings.json
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*",
"ConnectionStrings": {
"AmazonOrders": "Server=tcp:127.0.0.1,1433;Database=AmazonOrders;User=sa;Password=SQLserver123!;"
}
}
#using WebMatrix.Data
This is an old data access library intended solely for use with ASP.NET Web Pages, which is a pretty ancient (and virtually obsoleted) web development framework that targets the full .NET Framework. As such, it knows nothing about appsettings.json files which were introduced in .NET COre. The primary configuration mechanism for Web Pages is a web.config file and that's where this library looks for a connection string.
If I were you, I would remove WebMatrix.Data from your project (assuming it is actually a Razor Pages app, not Web Pages) and use Entity Framework Core instead. If it is a Web Pages app and that's the framework you want to work with, remove the appsettings.json file and add a connection string to the web.config file: https://www.connectionstrings.com/store-connection-string-in-webconfig/

Azure MobileServices Live/UAT/Dev environments

I have an azure mobile service that will go live at some point. So I need to create UAT and dev versions which would point to the UAT and dev databases. What I am struggling with is how to create these.
The namespace in my live, UAT and Dev databases need to be the same but if I create a new mobile service called myAppName_UAT, it's going to want to use MyAppName_UAT as the namespace and so will not find any of the tables.
Has anyone overcome this problem? Once the product goes live I'll need to be able to test the mobile apps against the Dev db without affecting live which surely must be a common scenario?
Any advice would be very gratefully received.
Edit: What I'm specifically after is how to manage the multiple environments within the Azure Portal. I can deploy all the other components of my UAT environment but I'm stuck on the mobile service.
I am not asking for a way for my applications to switch config files or to migrate data between databases. Does anyone have any experience of running azure applications with multiple components where they ran multiple mobile services?
Do you use a Version Control? For me, you just need to create branches to separate the 'UAT' and 'dev' versions.
About the databases:
You can use web.config transformations to switch the connection string between your databases.
http://msdn.microsoft.com/en-us/library/dd465326.aspx
How do I use Web.Config transform on my connection strings?
=================================================================================
Update:
Ok, now I understood what you want.
Create your two versions of mobile services:
1-Log in Windows Azure Management Portal (http://manage.windowsazure.com)
2-Create your test mobile services (if you already have then, skip this step):
2.1- New -> Compute -> Mobile Services
2.2- Url - MyMobileServicesTest
2.3- Database -> Create a new (test db).
3-Create your production mobile services (if you already have then, skip this step):
2.1- New -> Compute -> Mobile Services
2.2- Url - MyMobileServicesProduction
2.3- Database -> Create a new (production db).
Right now, you have two different versions.
Using Windows Azure SDK:
//public static MobileServiceClient MobileService = new MobileServiceClient(
// "AppUrl",
// "AppKey"
//);
Pay attention: AppUrl will be "MyMobileServicesTest.azure-mobile.net/" or "MyMobileServicesProduction.azure-mobile.net/". The app key, each environment will have it's own. You can store this settings in a config file and switch according to what you are doing.
More information:
http://www.windowsazure.com/en-us/develop/mobile/tutorials/get-started-with-data-dotnet/
Multiple mobile services can share the same database. You only need to specify the same schema name in web.config in each mobile service:
<appSettings>
<add key="MS_MobileServiceName" value="MyAppName" />
</appSettings>
Updated:
The setting above only works in localhost, but not after publishing to live.
Need to do the following trick in order to make it work. Just hard code the schema name into function OnModelCreating. This way the database schema name will not depend on the mobile service name any more:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
string schema = "MyAppName"; // ServiceSettingsDictionary.GetSchemaName();
if (!string.IsNullOrEmpty(schema))
{
modelBuilder.HasDefaultSchema(schema);
}
modelBuilder.Conventions.Add(
new AttributeToColumnAnnotationConvention<TableColumnAttribute, string>(
"ServiceTableColumn", (property, attributes) => attributes.Single().ColumnType.ToString()));
}

Local database on windows phone 8

I am a beginner at using windows phone SDK and I am trying to find the best method to create a local database to store app data on a windows phone 8 device. The data is to be stored from one page (not the main page) and retrieved and displayed from another page.
I have tried to use linq to sql and sqlite but as I am a beginner I do not know much about either method.
Can anyone recommend a good tutorial or a method which I can use?
PS: I am using Visual Studio Express 2012 for Windows Phone
Thanks
Here is the code for you to create the database
public class dbDataContext : DataContext
{
// Pass the connection string to the base class.
public dbDataContext(string connectionString)
: base(connectionString)
{ }
// Specify a single table for the idea items.
public Table<className> cn;
}
className is where all your Member variables will be stored with get set method.
then create the database as follows in the file (*.cs) where you want to create the database
private IdeaDataContext yourDb;
yourDb = new dbDataContext("Data Source=isostore:/anyname.sdf");
your database is created

How to populate data models with default data in Liferay?

I'm developing a portlet that is deployed as WAR. Database models are created by service builder. How can I insert initial data models to the database when the WAR is installed?
Add to the project a portal.properties file with the property:
application.startup.events=com.my.project.MyStartupAction
and impliment the startup import as extension of SimpleAction:
package com.my.project;
import com.liferay.portal.kernel.events.ActionException;
import com.liferay.portal.kernel.events.SimpleAction;
public class MyStartupAction extends SimpleAction {
#Override
public void run(String[] arg0) throws ActionException {
if(startupDataNotExist()) {
createStartupData();
}
}
...
You can do this either as StartupAction, executed on startup of the plugin (read: during deployment AND during subsequent server starts) or as an UpgradeAction.
A good example for this is the sevencogs-hook that comes with Liferay CE and has the source code included. This is implemented as an UpgradeAction - e.g. on first start your database content will be "upgraded" to contain the sevencogs sample data.
Also you can do it with UpgradingProcess. Here are step-by-step instructions

read list items from sharepoint 2010 server to silverlight web application (cross-domain)

on one server i have www with silverlight web application. In a diffrent place in the world is the secound server with sharepoint 2010 fondation. While client connects to www server, the silverloght web application goes to him, and then trying to read list elements from sharepoint. On client desktop comes Windows - login window, after client provide username and the password, application can read the list items.
Question:
how to login IN THIS CASE from silverlight application without user prompt.
part of my code (reads only list infromation):
SilverlightApplication2.listsWebService.ListsSoapClient lws = new SilverlightApplication2.listsWebService.ListsSoapClient();
public MainPage()
{
InitializeComponent();
lws.GetListCompleted += new EventHandler<listsWebService.GetListCompletedEventArgs>(lws_GetListCompleted);
lws.GetListAsync("PagesContent");
}
void lws_GetListCompleted(object sender, listsWebService.GetListCompletedEventArgs e)
{
deltaValue.Text = e.Result.Value;
}
It can be very confusing when solving Silverlight auth in Sharepoint. I know this is possible when using Forms authentication, through web service called autentication.asmx.
With Windows authentication, I always used current logged user (which is SL default behavior).
I don't know if that can be changed from inside SL..
Here is one article that may help...
Sorry for such answer... :)

Resources