user710502
user710502

Reputation: 11469

MobileServiceClient with different Model name than table name

Is there a way in Xamarin using MobileServiceClient to add an attribute to the class name to have a different table name?

i.e

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

But the table on the server is XCHX_Blogs

I would like to have my model class something like this

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

I cant seem to find a way to do this mapping in Xamarin forms (in the model).

Upvotes: 0

Views: 86

Answers (2)

Mayhem50
Mayhem50

Reputation: 305

You can use DataContractAttribute or JsonContainerAttribute or DataTableAttribute

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

or

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

or

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

Personaly, I prefer the last solution because naming is coherent with type of objects.

Upvotes: 0

Eric Hedstrom
Eric Hedstrom

Reputation: 1627

In order to do exactly what you are asking you'd have to pull the Mobile Service Client SDK source into your app (instead of using the Nuget) so that you could use the internal MobileServiceTable<T> constructor directly:

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

Alternatively you can use the non-generic MobileServiceTable, but then you'll have to handle the JSON de/serialization:

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

Upvotes: 1

Related Questions