gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 12:49:40 25 4
gpt4 key购买 nike

我使用 Visual Studio 创建了一个新表“TravauxDBs”,但是当我调用 PullAsync 时它返回计数 = 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;
}
}

我还为此添加了一个新 Controller ,只是用 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);
}

}

如果缺少某些内容或您需要更多详细信息,请告诉我。

提前谢谢你。

最佳答案

客户端类名需要与服务器 Controller 名匹配。所以,你的类应该被称为 Travaux 而不是 TravauxDB。或者,您可以使用属性 [TableName]在类里面。

关于c# - 无法使用 Azure Mobile Services .NET 从第二个表中读取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36037619/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com