简体   繁体   中英

Azure Mobile Services Identity Column Not Assigned While Offline

I'm using the Azure Mobile Services .NET backend with Xamarin.Forms and I'm having an issue with using int identity ID columns. When in an offline scenario, I can successfully add the record to the DB, but the CustomerId column which is an identity remains at 0. Once online, when I sync, the value is set by SQL server and populated on the mobile device.

As the ID is required as I also need to create related data in another table, the question is, how can I get/set an ID for an entity before it's sync'd with the server? I can't set it manually as it may clash with another client. I am suing an existing SQL database, so would prefer not to have to entirely change the scheme to change the ID to use strings or guid's.

    public class Customer
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int CustomerId { get; set; }

        public string Name { get; set; }

        ....
    }

    public async Task SaveCustomer(Customer item) {
        await CustomerTable.InsertAsync (item);
    }

The scenario you are mentioning is exactly why Azure Mobile Apps uses strings for IDs. If you use a string, you can generate a GUID for the ID and use that prior to sync.

The only way to get the Id that is generated by the database is to sync the table.

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