本文介绍了为什么Azure移动应用程序的数据模型中有字符串ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Azure移动应用中使用C#尝试学习它们.我创建了要链接到我的Azure SQL DB的模型,并创建了这样的DataObject:

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; }

}

请注意,我已注释掉上面的public int id;它使我在查询中出现重复的列错误.

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

最后,我使用新创建的Account DataObject创建了一个控制器.

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

因此,我运行了该应用程序并点击了表格/帐户"功能,它返回了零行(但是有数据,我可以向在Azure移动应用程序中使用的用户查询它).

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...)

首先,该ID被列出两次,一次是作为int(必须是我的),另一个是作为字符串的ID,我不知道那是哪里来的.

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.

此外,在数据库中,oGuid的类型为uniqueIdentifier;不是字符串.这可能是问题,也可能不是问题,因为我还无法测试.

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.

然后我的数据库中不存在其他列,包括CreatedAt(日期时间),UpdatedAt(日期时间),Version(字符串)和Deleted(位).

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.

我需要添加api测试中模型中列出的其他列吗?

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

我也测试过尝试调用/table/Account/3来加载特定的帐户,并且它不返回任何行...我猜这是模型不匹配,但是我不确定这是问题所在还是还有其他原因吗?我没有看到任何错误或警告.

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.

更新

我弄清楚了模型首先和Azure发生了什么,以及如何将Azure中的现有数据库附加到新代码.我将其发布在这里,希望它可以节省其他人的时间.这确实应该更容易做到.我还不喜欢Codefirst(现在),因为我想手动控制数据库...因此这使我使用数据库后端变得容易得多.

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.

首先,我创建了一个新项目(Azure Mobile App),然后在模型下,我右键单击该模型并添加->新实体数据模型,然后添加了azure db名称,密码,并为其指定了用户创建的配置文件名称",如下所示:在下面使用.如下所示,必须在web.config中编辑此连接.

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.

然后,我不得不在DataObjects中创建表的模型(没有MS必需的列),并在dataobject之外创建一个控制器.然后,我不得不编辑web.config并设置一个非实体数据库连接字符串:例如:

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"/>

最后,在MobileServiceContext中,我必须将DataObject模型映射到Azure sql中的表,并设置要使用的连接字符串,从默认的MS_TableConnectionString到web.config中的连接字符串.

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]";

,然后在OnModelCreating()下添加:

and under OnModelCreating() I added:

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

其中Account是我在DataObjects中创建的模型(类),而tblAccount是AzureDB中的表名.

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

推荐答案

EntityData抽象类包含其他字段-移动脱机同步有五个字段

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

  • Id(字符串-通常为GUID-必须是全局唯一的)
  • UpdatedAt(DateTimeOffset-通过数据库触发器自动维护-用于增量同步)
  • CreateAt(DateTimeOffset-用作进入数据库分区以优化读取的键,否则未使用)
  • 版本(字节[]-时间戳-用于乐观并发/冲突解决)
  • 已删除(布尔值-用于在删除记录时更新其他客户端-称为软删除).

您的客户端需要其客户端模型中的所有这些字段,但已删除"除外(除非请求,否则不会转移,并且会通过Mobile Apps SDK自动处理以清除已删除记录的脱机同步).

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:

  • Node (backend): https://azure.microsoft.com/en-us/documentation/articles/app-service-mobile-node-backend-how-to-use-server-sdk/
  • .NET (backend): https://shellmonger.com/2016/05/11/30-days-of-zumo-v2-azure-mobile-apps-day-19-asp-net-table-controllers/

这篇关于为什么Azure移动应用程序的数据模型中有字符串ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 05:52