模型名称与表名称不同的MobileServiceClient

时间:2018-09-06 02:55:00

标签: c# attributes azure-mobile-services

Xamarin中是否有一种使用MobileServiceClient的方法将属性添加到类名以具有不同的表名?

this._client = new MobileServiceClient("myurl");
this._blogTable = _client.GetTable<Blog>();

但是服务器上的表是XCHX_Blogs

我想让我的模型课像这样

[TableName("XCHX_Blogs")]
public class Blog {
   ...
}

我似乎找不到一种以Xamarin形式(在模型中)进行此映射的方法。

2 个答案:

答案 0 :(得分:1)

为了完全按照您的要求进行操作,您必须将Mobile Service Client SDK源拉到您的应用程序中(而不是使用Nuget),以便可以使用internal MobileServiceTable<T>直接构造函数:

this._blogTable = new MobileServiceTable<Blog>("XCHX_Blogs", this._client);

或者,您可以使用非通用的MobileServiceTable,但是随后必须处理JSON de / serialization:

this._blogTable = _client.GetTable("XCHX_Blogs");
var blog = new Blog();
await this._blogTable.InsertAsync(JObject.FromObject(blog));

答案 1 :(得分:0)

您可以使用DataContractAttribute或JsonContainerAttribute或DataTableAttribute

[DataContractAttribute(Name = "tableName")]
public class MyClass { ... }
IMobileServiceTable<MyClass> table = client.GetTable<MyClass>();

[JsonObjectAttribute(Title = "tableName")]
public class MyClass { ... }
IMobileServiceTable<MyClass> table = client.GetTable<MyClass>();

[DataTableAttribute("tableName")]
public class MyClass { ... }
IMobileServiceTable<MyClass> table = client.GetTable<MyClass>();

就个人而言,我更喜欢最后一种解决方案,因为命名与对象类型是一致的。

相关问题