模型名称与 table 名称不同的 MobileServiceClient
MobileServiceClient with different Model name than table name
在 Xamarin 中使用 MobileServiceClient 是否有办法将属性添加到 class 名称以具有不同的 table 名称?
即
this._client = new MobileServiceClient("myurl");
this._blogTable = _client.GetTable<Blog>();
但是服务器上的table是XCHX_Blogs
我想要我的模型 class 像这样
[TableName("XCHX_Blogs")]
public class Blog {
...
}
我似乎找不到在 Xamarin 表单(在模型中)中执行此映射的方法。
为了完全按照您的要求执行操作,您必须将移动服务客户端 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));
您可以使用 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>();
就个人而言,我更喜欢最后一种解决方案,因为命名与对象类型一致。
在 Xamarin 中使用 MobileServiceClient 是否有办法将属性添加到 class 名称以具有不同的 table 名称?
即
this._client = new MobileServiceClient("myurl");
this._blogTable = _client.GetTable<Blog>();
但是服务器上的table是XCHX_Blogs
我想要我的模型 class 像这样
[TableName("XCHX_Blogs")]
public class Blog {
...
}
我似乎找不到在 Xamarin 表单(在模型中)中执行此映射的方法。
为了完全按照您的要求执行操作,您必须将移动服务客户端 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));
您可以使用 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>();
就个人而言,我更喜欢最后一种解决方案,因为命名与对象类型一致。