After migrating database and mobile service the mobile service is still using old schema - azure

I needed to move an azure project to a new account. My azure project consists of a SQL database and a mobile service connected to that database.
I moved the database by backing up the database into a .bacpac file and importing it in a new Azure account. I recreated the mobile service manually. Because the mobile service needed a different name, I changed the schema on the imported tables by running:
ALTER SCHEMA [NewSchema]
TRANSFER [OldSchema].[TableName]
Now after migration the SELECT works. In the azure portal I can see all the tables have rows, and the mobile service can read from the database. The problem however is in insert, and in Custom API's of the service.
Those seem to still work with the old schema in mind:
Insert: Error: [Microsoft][SQL Server Native Client 10.0][SQL Server]Invalid object name '[OldSchema].[TableName]'. (SqlState: 42S02, Code: 208)
CustomAPI: Error: [Microsoft][SQL Server Native Client 10.0][SQL Server]Invalid object name '[OldSchema].[TableName]'.
There are no references to the old schema that I can find anywhere. Not in the codebase, not in the database.
I even went and executed
SELECT * FROM sys.database_principals
alter user <userName> WITH DEFAULT_SCHEMA = [NewSchema]
for all users.
I am out of ideas. What am I missing, where is it referencing the old schema?
Thank you for your help!
EDIT: I have just tried printing SELECT SCHEMA_NAME() within the Custom API I am trying to execute. It printed the correct, new, schema name. When I printed SELECT CURRENT_USER the correct user is printed, with the correct default schema. I am now more confused than ever.

Can you check your database triggers? There's a trigger that runs on Insert/Update/Delete that may still have a reference to your old schema.

Related

Rest Web API shows records in localserver, but not in IIS

I am trying to use Rest web API , to retrieve data.
When i am building and executing it using localserver, it is displaying the data.
But, when i publish it and use IIS to access it does not show any data. and shows this
"This XML file does not appear to have any style information associated with it. The document tree is shown below."
i am already using the same API for the functions of fileupload and they are working. Its just the matter of database, whenever i have to access database,it showing me the error above(This error means that no data is fetched).
Can you please help me with it?
Thanks in advance
The common reason behind the issue is iis user does not have enough permission to access the database.
To resolve the issue you can assign the database domain user which has enigh access to the database to the application pool by following below steps:
1)Open iis manager
2)select your application pool -> advanced setting.
3)Under Process Model, click on the “Identity” value and select “Custom account”.
enter your domain user name(DOMAIN\USERNAME) and password which user has to access to the database and click ok to apply the changes.
after applying changes select the application pool and click on the “Recycle”.
If you still face an issue you could refer below link for how to configure application pool user in SQL server.
https://forums.iis.net/post/2159167.aspx

"Login failed for user" as "The underlying provider failed on Open" in Azure SQL

I am trying to build a small Web API that uses an Azure SQL Database. The database functionality is exposed via Stored Procedures and in order to call them, I am using Entity Framework for .NET, version 6.4.4 as it gives me access to the .edmx Data Model. I have properly configured the EF so that I can see the SPs and I've added one of them to my model. In my controller, I execute this piece of code:
var dbContext = new MyDbContext();
var result = dbContext.MyStoredProc();
I get an exception when the "MyStoredProc()" method gets called. The exception that is thrown is this:
For now, the connection string is configured to use SQL Authentication, so the user listed in the exception is the user that is configured in the connection string.
I can call the same SP no problems if I use SQL Server Management Studio, within a session open with the same user name and password as I have configured in the connection string.
Why is my application throwing up that exception? Are there specific privileges that I must elevate, in order for the connection to the database to be opened from my Web API?
Any help would be appreciated.
Cheers,
Eddie
In projects that use Entity Framework, an error message usually reports that there is no Login failed for user. At this time, we need to check connectionStrings in ʻApp.config`.
In order to help more forum users, it is recommended that everyone can refer to the following tutorial.
Entity Framework Tutorial

Azure Cosmos DB - Access different data per user (SQL)

I am new to web development, so the question might be trivial.
I have some physical devices, which will transmit humidity (and other stuff) to Azure Cosmos DB. I want users to be able to read their devices only(through a graph in a web app). All devices end up the the same collection.
I have set up a system in Azure in the following way:
Device -> Azure IoT-Hub -> Azure functions -> Azure Cosmos DB.
The web app uses Azure B2C for users to be able to sign in, but how do i "assign" device-id´s to a user? I have read that it is possible to create access tokens with specific access to certain partition keys, but how do I store the information with access permissions for each user? Do I need a new database for storing this information only?
In the beginning it is fine if I have to grant permissions manually.
General info:
The data in the Azure cosmos db are using device-id as the partition key.
Users can have more than one device.
The web app is running ASP.NET core (I am new here).
The web app requires log-in to be accessed.
I am coding in Visual Studio 2019, and have used the example with a build-in authorization.
Thank you for your help.
NOTE: It might be too much to ask for, but a general explanation together with a code example would be perfect for me :)
UPDATE:
I managed to fetch the email of the logged in user:
#using System.Security.Claims; // for using ClaimsIdentity
// Get user information
var userEmailAddress = ((ClaimsIdentity)User.Identity).FindFirst("emails").Value;
My initial idea is then to have a database containing: "Email" and "accessible devices". I guess this has to be a separate database since i need the Time-To-Live parameter in cosmos db.
You might be able to achieve what you are looking for by using Resource Tokens.
As in this sample from the documentation, you can assign permissions by partition key:
//Create a user.
Database database = benchmark.client.GetDatabase("SalesDatabase");
User user = await database.CreateUserAsync("User 1");
// Create a permission on a container and specific partition key value
Container container = client.GetContainer("SalesDatabase", "OrdersContainer");
user.CreatePermissionAsync(
new PermissionProperties(
id: "permissionUser1Orders",
permissionMode: PermissionMode.All,
container: benchmark.container,
resourcePartitionKey: new PartitionKey("012345")));
Alright, I have been busy with other projects, but i finally came back to this issue.
So basically the reason why I wanted a separate database was because I need the TTL on the Azure Cosmos-DB, and I do not want to delete the user permissions.
It turns out that it is possible to overrule the database's default TTL by setting a "ttl" field in the document itself. Thereby it is possible to exclude documents from being deleted.
So the basic solution is:
Create a documents with a "ttl" parameter set to -1 to exclude it from being deleted. Then this document can contain information about which users have access to what.

Database name for found

I am trying to migrate my data from Parse to Azure DocumentDB. I copied the Connection String from the Portal to the Parse DashBoard for migration but I am getting error: 'You need to provide a database name'. What am I doing wrong?
mongodb://server:password==#database_name.documents.azure.com:port/?ssl=true
The issue is that, with DocumentDB, the top-level name (in your example, database_name) is the account name. Within that account, you have one or more databases. For example, here's my DocumentDB account, with a Parse database:
That database name needs to be added to your connection string. Using your example, it would be added like this:
mongodb://server:password==#database_name.documents.azure.com:port/parse?ssl=true
Notice the /parse part at the end: That's the database within the DocumentDB account.
What you're calling #database_name in your connection string is, in fact, the DocumentDB account name. So really, the proper way to think about it is:
mongodb://server:password==#docdb_account_name.documents.azure.com:port/docdb_database_name?ssl=true

After doing PullAsync() on a table, I am unable to update the table data - Offline data sync in .Net Mobile service

We are developing a windows store 8.1 app, In that we implemented Offline data sync using Azure mobile Service(.net backend).
We are using Mobile service with On-Premise SQL Server with existing database using Code first migrations.
We have a USER table which stores user emailId and offlinePin in the database.
We also implemented AAD single sign on in the app. For all active directory users those who are going to use the app, we added their email ids to the User table with out offlinePin value.
In client app, We are calling the following line based on the user who log in to the app using AAD single sign on.
Declaration:
private IMobileServiceSyncTable<Users> usertable= App.MobileService.GetSyncTable<Users>();
await usertable.PullAsync("SyncLoggedInUserInfo", usertable.Where(user => user.Email == App.UserEmail));
Now for the User who log in based on his/her mail Id we are pulling their information from USER table using above line.
If the logged in user don't have an offline pin then the app will prompt the user to create one and save that into the local SQLite USER table.
For updating the user offlinePin we are calling the following lines
var userInfo = await usertable.Where(x => x.Email == App.UserEmail).ToListAsync();
if (userInfo .FirstOrDefault()!=null)
{
var emp = userInfo .FirstOrDefault();
emp.OfflinePin = pinpswrdbx.Password;
await usertable.UpdateAsync(emp);
}
After updating done we are pushing those changes to the Server.
await App.MobileService.SyncContext.PushAsync();
Here the issue is, the line calling for update the offline pin is not working, means
await usertable.UpdateAsync(emp); is not updating the respective employee information in local table. We are not getting any exception here, it's executing successfully but the offline pin column value is not updating with the user entered pin value.
It is happening only for the rows of data added directly on database and synced to local SQLite DB which are not created within the app, if the record is inserted/created with in the app then that data is updating and am able to push those changes to server as well.
I have to use the existing database and tables which already having data in it and the app should be able to update the data and push the changes back to the server DB.
Can anybody help me where I am missing or doing wrong?
The problem is with the database, it is having case sensitive collation. I used fiddler to track what exactly the mobile service doing while pushing result to server, then I found that in the request URL all the stating letters of the table columns are automatically converting in to capital letters, but in database they are in small letters. May be due to this the data wasn't updated in database and roll backing in local DB as well.
To overcome this issue I had written the below line of code in DBContext class in mobile service project and did publish.
modelBuilder.Entity<User>().Property(p => p.Offlinepin).HasColumnName("offlinepin");
After this updating is working. I don't know why Mobile service did not show any error message when push failed instead it roll back the local values.

Resources