简体   繁体   中英

Why is there a string ID in the data model of Azure Mobile Apps?

I'm working the C# in Azure Mobile Apps trying to learn them. I created the Model to link to my Azure SQL DB, created a DataObject like this:

public class Account : EntityData
{
    //public int id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string PhoneNumber { get; set; }
    public string Password { get; set; }
    public DateTime dtCreated { get; set; }
    public Guid oGuid { get; set; }

}

Notice that I commented out the public int id above; it was giving me a duplicate column error on the query.

Finally, I created a controller using the newly created Account DataObject.

So I ran the application and hit the "tables/Account" function and it returned zero rows (but there is data and I can query it with the user I'm using in the azure mobile app).

I then noticed the model schema as this:

[
  {
    "id": 0,
    "FirstName": "string",
    "LastName": "string",
    "PhoneNumber": "string",
    "Password": "string",
    "dtCreated": "2016-07-06T17:45:47.114Z",
    "oGuid": "string",
    "Id": "string",
    "Version": "string",
    "CreatedAt": "2016-07-06T17:45:47.114Z",
    "UpdatedAt": "2016-07-06T17:45:47.114Z",
    "Deleted": true
  }
]

There's a couple of issues I see with the configured model (and I don't know where some of the columns are coming from...)

First, the id is listed twice, once as an int (which has to be mine) and another id as string and I have no idea where that came from.

Also, in the DB, the oGuid is of type uniqueIdentifier; not string. This may or may not be an issue because I can't test yet.

Then there are the other columns that just do not exist in my DB including CreatedAt (datetime), UpdatedAt (datetime), Version (string) and Deleted (bit).

I'm thinking the issue / reason why I'm not getting any data back from that call is that there is a data mismatch.

Do I need to add the other columns that are listed in the model in the api test?

I've also tested trying to call the /table/Account/3 to load a specific account and it returns no rows... I'm guessing it's a model mismatch but I'm not sure if that's the issue or something else causing it? I'm not seeing any errors or warnings.


Update

I figured out what is going on with model first and Azure and how to attach an existing DB in Azure to new code. I'm going to post this here for the hopes that it saves other's time. This really should have been easier to do. I'm not a fan of codefirst (yet) as I like to control the DB by hand... So this makes it a lot easier for me to work with the db backend.

First I created a new project (Azure Mobile App) then under models I right clicked the model and add->new entity data model then added in the azure db name, password and gave it my "user created profile name" as used below. This connection must be edited in the web.config as shown below.

I then had to create the model for the table in DataObjects (without the MS required columns) and create a controller off of the dataobject. I then had to edit the web.config and set a non-entity DB connection string: eg:

<add name="[user created preset name]" providerName="System.Data.SqlClient" connectionString="Server=[Azuredb server connection];initial catalog=[DBName];persist security info=True;user id=[user];password=[pass];MultipleActiveResultSets=True"/>

Finally, in the MobileServiceContext, I had to map the DataObject model to the table in Azure sql and set the connection string to use from the default MS_TableConnectionString to the connectionstring in web.config.

    private const string connectionStringName = "Name=[user created preset name]";

and under OnModelCreating() I added:

    modelBuilder.Entity<Account>().ToTable("tblAccount");

Where Account was the model (class) I created in DataObjects and the tblAccount is the table name in AzureDB.

The EntityData abstract class contains the additional fields - there are five fields for Mobile offline sync

  • Id (a string - normally a GUID - must be globally unique)
  • UpdatedAt (DateTimeOffset - maintained automatically via a database trigger - used for incremental sync)
  • CreateAt (DateTimeOffset - used as the key into the database partition to optimize reading, but unused otherwise)
  • Version (a byte[] - timestamp - used for optimistic concurrency / conflict resolution)
  • Deleted (a Boolean - used to update other clients when a record is deleted - known as soft delete).

Your client needs all these fields in its client model except for Deleted (which isn't transferred unless requested and is dealt with automatically via the Mobile Apps SDK for clearing the offline sync of deleted records).

You haven't said what languages are in use on backend or frontend. However, logging is available in both cases - you just have to turn it on, capture the exceptions, etc. Some references for you:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM