简体   繁体   English

为什么Azure移动应用程序的数据模型中有字符串ID?

[英]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. 我正在Azure移动应用中使用C#尝试学习它们。 I created the Model to link to my Azure SQL DB, created a DataObject like this: 我创建了模型以链接到我的Azure SQL DB,并创建了一个DataObject,如下所示:

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; 注意,我在上面注释掉了公共int id。 it was giving me a duplicate column error on the query. 这给了我查询重复的列错误。

Finally, I created a controller using the newly created Account DataObject. 最后,我使用新创建的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). 因此,我运行了该应用程序并单击了“表/帐户”功能,它返回了零行(但是有数据,我可以向在Azure移动应用程序中使用的用户查询它)。

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. 首先,该ID被列出两次,一次是int(必须是我的),另一次是字符串,我不知道它从何而来。

Also, in the DB, the oGuid is of type uniqueIdentifier; 同样,在数据库中,oGuid的类型为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). 然后还有我的数据库中不存在的其他列,包括CreatedAt(日期时间),UpdatedAt(日期时间),Version(字符串)和Deleted(位)。

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? 我是否需要添加api测试中模型中列出的其他列?

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? 我还测试过尝试调用/ table / Account / 3来加载特定帐户,并且它不返回任何行...我猜这是模型不匹配,但是我不确定这是问题还是其他原因它? 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. 我了解了模型优先和Azure的情况,以及如何将Azure中的现有数据库附加到新代码。 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. 首先,我创建了一个新项目(Azure Mobile App),然后在模型下,我右键单击该模型并添加->新实体数据模型,然后添加了azure db名称,密码,并为其指定了“用户创建的配置文件名称”,如下所示。 This connection must be edited in the web.config as shown below. 如下所示,必须在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. 然后,我不得不在DataObjects中创建表的模型(没有MS必需的列),并根据dataobject创建一个控制器。 I then had to edit the web.config and set a non-entity DB connection string: eg: 然后,我不得不编辑web.config并设置一个非实体数据库连接字符串:例如:

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

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

and under OnModelCreating() I added: 在OnModelCreating()下,我添加了:

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

Where Account was the model (class) I created in DataObjects and the tblAccount is the table name in AzureDB. 其中Account是我在DataObjects中创建的模型(类),而tblAccount是AzureDB中的表名。

The EntityData abstract class contains the additional fields - there are five fields for Mobile offline sync EntityData抽象类包含其他字段-移动脱机同步有五个字段

  • Id (a string - normally a GUID - must be globally unique) ID(字符串-通常为GUID-必须是全局唯一的)
  • UpdatedAt (DateTimeOffset - maintained automatically via a database trigger - used for incremental sync) UpdatedAt(DateTimeOffset-通过数据库触发器自动维护-用于增量同步)
  • CreateAt (DateTimeOffset - used as the key into the database partition to optimize reading, but unused otherwise) CreateAt(DateTimeOffset-用作进入数据库分区以优化读取的键,但未使用)
  • 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). 您的客户端需要其客户端模型中的所有这些字段,但“已删除”(除非请求,否则不会传输,并且会通过Mobile Apps SDK自动处理以清除已删除记录的脱机同步)。

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: 但是,在两种情况下都可以使用日志记录-您只需将其打开,捕获异常等即可。为您提供一些参考:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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