无法使用Azure Mobile Services .NET从第二个表读取数据

时间:2016-03-16 13:50:00

标签: c# xamarin.android azure-mobile-services

我创建了一个新表" TravauxDBs"使用Visual Studio,但是当我调用PullAsync时,它返回count = 0,所以我猜它是空的。然后我收到错误"找不到"。

使用基本表ToDoItems一切正常,我对这个新表使用相同的代码。

我的项目移动端:

public class TravauxDB
{
    public string Id { get; set; }

    [JsonProperty(PropertyName = "nomtravail")]
    public string NomTravail { get; set; }

    [JsonProperty(PropertyName = "voie")]
    public string Voie { get; set; }

    [JsonProperty(PropertyName = "pk")]
    public string PK { get; set; }

    [JsonProperty(PropertyName = "quantity")]
    public string Quantity { get; set; }

    [JsonProperty(PropertyName = "unite")]
    public string Unite { get; set; }

    [JsonProperty(PropertyName = "observation")]
    public string Observation { get; set; }

    [JsonProperty(PropertyName = "idchantier")]
    public string IdChantier { get; set; }

}

服务方:

public class TravauxDB : EntityData
{
    public string NomTravail { get; set; }

    public string Voie { get; set; }

    public string PK { get; set; }

    public string Quantity { get; set; }

    public string Unite { get; set; }

    public string Observation { get; set; }

    public string IdChantier { get; set; }

}

活动:

public class Travaux : ActionBarActivity
{
    private Android.Support.V7.Widget.SearchView _searchView3;
    private ListView _listView3;
    private ArrayAdapter _adapter3;

    private MobileServiceClient client;

    private IMobileServiceSyncTable<TravauxDB> TravauxTable;

    const string applicationURL = @"myurl";
    const string applicationKey = @"mykey";

    const string localDbFilename = "localstore.db";

    public List<string> travaux { get; private set; }

    protected override async void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        SetContentView(Resource.Layout.Layout_travaux);

        CurrentPlatform.Init();

        client = new MobileServiceClient(applicationURL, applicationKey);
        await InitLocalStoreAsync();

        TravauxTable = client.GetSyncTable<TravauxDB>();

        await SyncAsync();

        List<string> travaux = await ItemsOnly();

        _listView3 = FindViewById<ListView>(Resource.Id.listView3);
        _adapter3 = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItem1, travaux);
        _listView3.Adapter = _adapter3;
        _listView3.ItemClick += _listView3_ItemClick;

    }

    void _listView3_ItemClick(object sender, AdapterView.ItemClickEventArgs e)
    {
        int pos = e.Position;

        Intent b = new Intent(this, typeof(Travail));
        Bundle bundle = new Bundle();
        bundle.PutInt("Ntravail", pos);
        b.PutExtras(bundle);
        StartActivity(b);
    }

    public override bool OnCreateOptionsMenu(IMenu menu)
    {
        MenuInflater.Inflate(Resource.Menu.main, menu);

        var item = menu.FindItem(Resource.Id.action_search);

        var searchItem = MenuItemCompat.GetActionView(item);
        _searchView3 = searchItem.JavaCast<Android.Support.V7.Widget.SearchView>();

        _searchView3.QueryTextChange += (s, e) => _adapter3.Filter.InvokeFilter(e.NewText);

        _searchView3.QueryTextSubmit += (s, e) =>
        {
            Toast.MakeText(this, "Searched for: " + e.Query, ToastLength.Short).Show();
            e.Handled = true;
        };

        return true;
    }

    private void CreateAndShowDialog(Exception exception, String title)
    {
        CreateAndShowDialog(exception.Message, title);
    }

    private void CreateAndShowDialog(string message, string title)
    {
        Android.App.AlertDialog.Builder builder = new Android.App.AlertDialog.Builder(this);

        builder.SetMessage(message);
        builder.SetTitle(title);
        builder.Create().Show();
    }

    private async Task InitLocalStoreAsync()
    {
        string path = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), localDbFilename);

        if (!File.Exists(path))
        {
            File.Create(path).Dispose();
        }

        var store = new MobileServiceSQLiteStore(path);
        store.DefineTable<TravauxDB>();

        await client.SyncContext.InitializeAsync(store);
    }

    private async Task SyncAsync()
    {
        try
        {
            await client.SyncContext.PushAsync();
            await TravauxTable.PullAsync("alltravaux", TravauxTable.CreateQuery()); // query ID is used for incremental sync
        }
        catch (Java.Net.MalformedURLException)
        {
            CreateAndShowDialog(new Exception("There was an error creating the Mobile Service. Verify the URL"), "Error");
        }
        catch (Exception e)
        {
            CreateAndShowDialog(e, "Error");
        }
    }

    protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
    {
        base.OnActivityResult(requestCode, resultCode, data);
        AuthenticationAgentContinuationHelper.SetAuthenticationAgentContinuationEventArgs(requestCode, resultCode, data);
    }

    public async Task<List<string>> ItemsOnly()
    {
        try
        {

            List<string> travaux = await TravauxTable
                .Select(tr => tr.NomTravail).ToListAsync();
            return travaux;

        }
        catch (MobileServiceInvalidOperationException e)
        {
            Console.Error.WriteLine(@"ERROR {0}", e.Message);
            return null;
        }
    }

我还为此添加了一个新的控制器,只是用TravauxDB替换了ToDoItem:

public class TravauxController : TableController<TravauxDB>
{

    protected override void Initialize(HttpControllerContext controllerContext)
    {
        base.Initialize(controllerContext);
        ChantierContext context = new ChantierContext();
        DomainManager = new EntityDomainManager<TravauxDB>(context, Request, Services);
    }

    // GET tables/TodoItem
    public IQueryable<TravauxDB> GetAllTravauxDB()
    {
        return Query();
    }

    // GET tables/TodoItem/48D68C86-6EA6-4C25-AA33-223FC9A27959
    public SingleResult<TravauxDB> GetTravauxDB(string id)
    {
        return Lookup(id);
    }

    // PATCH tables/TodoItem/48D68C86-6EA6-4C25-AA33-223FC9A27959
    public Task<TravauxDB> PatchTravauxDB(string id, Delta<TravauxDB> patch)
    {
        return UpdateAsync(id, patch);
    }

    // POST tables/TodoItem
    public async Task<IHttpActionResult> PostTravauxDB(TravauxDB item)
    {
        TravauxDB current = await InsertAsync(item);
        return CreatedAtRoute("Tables", new { id = current.Id }, current);
    }

    // DELETE tables/TodoItem/48D68C86-6EA6-4C25-AA33-223FC9A27959
    public Task DeleteTravauxDB(string id)
    {
        return DeleteAsync(id);
    }

}

如果缺少某些内容或需要更多详细信息,请与我们联系。

先谢谢你。

1 个答案:

答案 0 :(得分:1)

客户端类名称需要与服务器控制器名称匹配。因此,您的班级应该被称为Travaux而不是TravauxDB。或者,您可以在类上使用属性[TableName]