C# 客户端模型应使用哪些数据类型与Azure mobile apps SDK进行脱机同步?

C# 客户端模型应使用哪些数据类型与Azure mobile apps SDK进行脱机同步?,c#,azure,azure-mobile-services,C#,Azure,Azure Mobile Services,我正在使用Azure的移动应用服务SDK中的离线同步功能 我知道SDK最近有很多变化。我想根据最新的规范定义客户机模型,但不确定使用哪种类型 这些是脱机同步元数据属性,通常见于大多数示例/教程: [JsonProperty(PropertyName = "id")] public string Id { get; set; } [Version] public string Version { get; set; } [CreatedAt] public DateTimeOffset Cre

我正在使用Azure的移动应用服务SDK中的离线同步功能

我知道SDK最近有很多变化。我想根据最新的规范定义客户机模型,但不确定使用哪种类型

这些是脱机同步元数据属性,通常见于大多数示例/教程:

[JsonProperty(PropertyName = "id")]
public string Id { get; set; }

[Version]
public string Version { get; set; }

[CreatedAt]
public DateTimeOffset CreatedAt { get; set; }

[UpdatedAt]
public DateTimeOffset UpdatedAt { get; set; }

[Deleted]
public bool Deleted { get; set; }
但是一些和,以及各种官方(太多!)使用类型的组合

所以我在不同的地方也看到了这一点:

[JsonProperty(PropertyName = "id")]
public Guid Id { get; set; }              // Guid is used here, not string

[Version]
[JsonProperty(PropertyName = "version")]  // Needed? I assume the attribute is enough
public byte[] Version { get; set; }       // byte[] is used here, not string
在封面下,一切都是通过REST调用完成的,因此字符串也是如此。因此,我假设客户端SDK执行各种类型转换

但我不想让我的应用程序在将来的某个时候,当事情发生变化时,会莫名其妙地爆炸。那么我应该使用哪些官方支持的类型呢?

看起来您几乎可以对
id
列使用任何类型,只要它是唯一的。(感觉像NoSQL…)我已经决定使用Guid,所以所有实体在创建时都是一致的(无需等待服务器的响应)

我已经为
version
列决定了一个字节数组,因为它似乎正是从服务器返回的


编辑:这是完全错误的。请参阅下面的评论和上面新的已接受答案。

客户端SDK只需要一个字符串Id字段(该字段应命名为“Id”,除非您想跳过一系列限制)。字符串的值可以是任何唯一的字符串。默认情况下,服务器SDK使用字符串转换的GUID。使用脱机同步时,除非指定了ID,否则客户端SDK还会生成字符串GUID。当使用联机
imobileservice表
API时,客户端让服务器生成ID

其余字段是可选的,应该具有列出的类型,或者可以转换为这些类型

以下是您的数据模型,并对各个字段进行了注释:

// required, should be called Id, *must* be of type string when using offline sync
// When not using offline sync, should be string-convertible.
// When using offline sync, the client SDK uses a new string-converted GUID for this, unless 
// an ID is specified.
// When using offline sync, Ids are not mutable, so use something that can be client generated
public string Id { get; set; }

// optional. Using a Version field opts you into optimistic concurrency, where 
// the server will reject updates that are done against an older version
// of an object. This means you need a conflict handler.
// To use a client-wins policy, remove this property from your client object
[Version]
public string Version { get; set; }

// optional. Cannot be set on the client, will be sent from the server
[CreatedAt]
public DateTimeOffset CreatedAt { get; set; }

// optional. Cannot be set on the client, will be sent from the server
[UpdatedAt]
public DateTimeOffset UpdatedAt { get; set; }

// should generally not be used in the client object at all. This field tracks 
// which objects have been deleted so that they are automatically purged
// from the client's offline sync store
[Deleted]
public bool Deleted { get; set; }

从技术上讲,不是这样。您的Id字段在所有客户端中必须是唯一的-两个客户端引用具有相同Id的两个不同对象的风险不得存在。Guid是一个不错的选择。啊,是的,这很有意义。两个用户可能决定使用相同的电子邮件地址!谢谢你的澄清。我正在使用您推荐的Guid(而不是字符串)。这是一个很好的参考。。。所有不同的问题都集中在一个地方。。。非常感谢。但是关于
Id
——根据上面的评论,我可以使用
Guid
(而不是
string
)。对于
版本
我想使用
字节[]
(而不是
字符串
)。对于我的数据模型来说似乎更自然,但是,我这样做是自找麻烦吗?是的,因为您需要字符串转换为GUID,而服务器不需要GUID@adrian hall的意思是您应该使用GUID的字符串表示形式,而不是实际的GUID类型。@hbob我在答案中添加了有关客户端ID生成的更多详细信息。@hbob没错,响应可以作为任意字符串发回(或者可以通过直接更新数据库来创建新记录),并且该字符串可能无法转换为GUID。另外,如果您在默认的本地存储实现中使用脱机同步,我相信ID的GUID可能会失败或导致细微的错误。对于版本,字节[]可能会起作用,但字符串更安全。您不需要在客户端将其作为字节数组进行操作。