我正在使用Windows Phone 8.1应用,但我无法让客户端的模型对象从我在Azure上托管的数据库中获取其值。我写的代码似乎工作(至少迭代正确),但我的问题是Bars对象是空的。我不知道如何让他们获取存储在db(barID,barName等)中的值。我已经构建了一个我已发布到Azure网站的工作服务项目。该项目包含我的DTO,上下文和控制器。我的客户端项目包含模型和视图。我需要改变我的架构吗?如果是这样,我应该如何改变它?这是我的控制器中调用的代码:
public class BarsController : ApiController
{
private GatoradeShowerDB db = new GatoradeShowerDB();
// GET: api/Bars
// [Route("http://gatoradeshower.azurewebsites.net/api/bars/All")]
public IQueryable<Bar> GetBars()
{
return db.Bars;
}
// GET: api/Bars/5
[ResponseType(typeof(Bar))]
public async Task<IHttpActionResult> GetBar(int id)
{
Bar bar = await db.Bars.FindAsync(id);
if (bar == null)
{
return NotFound();
}
// return Ok(bar);
return Ok(bar);
}
// PUT: api/Bars/5
[ResponseType(typeof(void))]
public async Task<IHttpActionResult> PutBar(int id, Bar bar)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
if (id != bar.BarID)
{
return BadRequest();
}
db.Entry(bar).State = EntityState.Modified;
try
{
await db.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!BarExists(id))
{
return NotFound();
}
else
{
throw;
}
}
return StatusCode(HttpStatusCode.NoContent);
}
// POST: api/Bars
[ResponseType(typeof(Bar))]
public async Task<IHttpActionResult> PostBar(Bar bar)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
db.Bars.Add(bar);
await db.SaveChangesAsync();
return CreatedAtRoute("DefaultApi", new { id = bar.BarID }, bar);
}
// DELETE: api/Bars/5
[ResponseType(typeof(Bar))]
public async Task<IHttpActionResult> DeleteBar(int id)
{
Bar bar = await db.Bars.FindAsync(id);
if (bar == null)
{
return NotFound();
}
db.Bars.Remove(bar);
await db.SaveChangesAsync();
return Ok(bar);
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
db.Dispose();
}
base.Dispose(disposing);
}
private bool BarExists(int id)
{
return db.Bars.Count(e => e.BarID == id) > 0;
}
}
这是我的DTO代码:
public partial class Bar
{
public int BarID { get; set; }
[Required]
[StringLength(255)]
public string BarName { get; set; }
[Required]
[StringLength(255)]
public string BarStreet { get; set; }
[Required]
[StringLength(50)]
public string BarCity { get; set; }
[Required]
[StringLength(50)]
public string BarState { get; set; }
[Required]
[StringLength(50)]
public string BarPhone { get; set; }
[Required]
[StringLength(50)]
public string BarZip { get; set; }
}
这是我的上下文代码:
public partial class GatoradeShowerDB : DbContext
{
public GatoradeShowerDB()
: base("name=GatoradeShowerDB")
{
}
public virtual DbSet<Bar> Bars { get; set; }
public virtual DbSet<Event> Events { get; set; }
public virtual DbSet<Game> Games { get; set; }
public virtual DbSet<Group> Groups { get; set; }
public virtual DbSet<Member> Members { get; set; }
public virtual DbSet<Stadium> Stadia { get; set; }
public virtual DbSet<Team> Teams { get; set; }
public virtual DbSet<TeamsTest> TeamsTests { get; set; }
public virtual DbSet<WatchParty> WatchParties { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Game>()
.Property(e => e.GameTime)
.IsFixedLength();
modelBuilder.Entity<TeamsTest>()
.Property(e => e.teamName)
.IsUnicode(false);
}
}
这是我的xaml.cs文件(视图)中的代码:
public async void GetBars()
{
using (var client = new HttpClient())
{
barsListBox.Items.Add("using block entered");
client.BaseAddress = new Uri("http://nflff.azurewebsites.net");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
barsListBox.Items.Add("client's defaultrequestheaders done");
HttpResponseMessage response = await client.GetAsync("api/bars");
barsListBox.Items.Add("httpresponsemessage object created with getasync");
barsListBox.Items.Add(response.Content.ToString());
if (response.IsSuccessStatusCode)
{
barsListBox.Items.Add("if entered");
IList<Bars> bars = await response.Content.ReadAsAsync<IList<Bars>>();
//successfully retrieves all bars items but need to format the printing
//Wait... How can you instantiate a list ... to read itself? (Am I wrong) Nothing is being stored?
//barsListBox.Items.Add(bars); //this just prints System.Collection.Generic...
barsListBox.Items.Add(bars.Count); //list contains correct number of items
for (int i = 0; i < bars.Count; i++)
{
barsListBox.Items.Add("for entered"); //iterating correct number of times
barsListBox.Items.Add(i); //when printing bar[i], just prints empty object
}
//foreach (var bar in bars)
//{
// barsListBox.Items.Add("foreach entered"); //not entering foreach
// barsListBox.Items.Add(bar.ToString());
//}
barsListBox.Items.Add("after foreach loop"); //getting printed
}
barsListBox.Items.Add("after if");
}
}
这是我(客户端)模型的代码:
class Bars
{
private int barID { get; set; }
private string barName { get; set; }
private string barStreet { get; set; }
private string barCity { get; set; }
private string barState { get; set; }
private string barPhone { get; set; }
private string barZip { get; set; }
//public Bars()
//{
// //empty constructor
//}
public override string ToString()
{
return "Bar: " + barID.ToString() +" " + barName.ToString() + " Phone: " + barPhone; //model obj not obtaining any values
}
}
我完全失去了,因为这是我第一次使用托管数据库创建移动应用。目前,我在我的视图中获得了从ReadAsAsync调用返回的正确数量的条形项,但它们全部为空。如何让这些Bars对象包含其正确的值?我的数据库是一个SQL数据库,在Azure网站上托管了.NET后端。
答案 0 :(得分:0)
我发现了我的问题。我的代码实际上正常工作。问题在于,当我将数据库部署到Azure网站时,它创建了一个具有相同表格和格式但没有任何数据的新数据库。当我通过localhost运行我的web api时,它运行正常,因为它使用的是一个包含我的示例数据的数据库,但托管数据库并没有显示任何内容。为了解决这个问题,我将我的示例数据导入到新的托管数据库中,一切正常。