在 Azure 行動應用後端專案,新增差旅費用頁面需要用的資料表
Enable-Migrations
Checking if the context targets an existing database...
Detected database created with a database initializer. Scaffolded migration '201702111723152_InitialCreate' corresponding to existing database. To use an automatic migration instead, delete the Migrations folder and re-run Enable-Migrations specifying the -EnableAutomaticMigrations parameter.
Code First Migrations enabled for project XamarinAzureDayService.
Add-Migration Initial
Scaffolding migration 'Initial'.
The Designer Code for this migration file includes a snapshot of your current Code First model. This snapshot is used to calculate the changes to your model when you scaffold the next migration. If you make additional changes to your model that you want to include in this migration, then you can re-scaffold it by running 'Add-Migration Initial' again.
using System.Data.Entity.Migrations;
using XamarinAzureDayService.Migrations;
var migrator = new DbMigrator(new Migrations.Configuration());
migrator.Update();
建立資料傳輸物件 (DTO) 類別
- 為了要開始產生一個我們自訂的資料表,我們需要定義資料表控制器, 設定資料表控制器需要三個步驟︰
public class BusinessTripExpense : EntityData
{
public DateTime 出差日期 { get; set; }
public string 類型 { get; set; }
public string 項目名稱 { get; set; }
public string 地點 { get; set; }
public double 費用 { get; set; }
public string 備註 { get; set; }
public bool 國內外 { get; set; }
public bool 是否有單據 { get; set; }
}
EntityData 類別
public bool Deleted { get; set; }
public abstract class EntityData : ITableData
{
protected EntityData();
[System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedAttribute]
[Index(IsClustered = true)]
[TableColumn(TableColumnType.CreatedAt)]
public DateTimeOffset? CreatedAt { get; set; }
[TableColumn(TableColumnType.Deleted)]
public bool Deleted { get; set; }
[System.ComponentModel.DataAnnotations.KeyAttribute]
[TableColumn(TableColumnType.Id)]
public string Id { get; set; }
[System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedAttribute]
[TableColumn(TableColumnType.UpdatedAt)]
public DateTimeOffset? UpdatedAt { get; set; }
[TableColumn(TableColumnType.Version)]
[System.ComponentModel.DataAnnotations.TimestampAttribute]
public byte[] Version { get; set; }
}
建立資料表控制器。
使用 Azure 行動應用程式資料表控制器
public class BusinessTripExpenseController : TableController<BusinessTripExpense>
{
protected override void Initialize(HttpControllerContext controllerContext)
{
base.Initialize(controllerContext);
XamarinAzureDayContext context = new XamarinAzureDayContext();
DomainManager = new EntityDomainManager<BusinessTripExpense>(context, Request);
}
// GET tables/BusinessTripExpense
public IQueryable<BusinessTripExpense> GetAllBusinessTripExpense()
{
return Query();
}
// GET tables/BusinessTripExpense/48D68C86-6EA6-4C25-AA33-223FC9A27959
public SingleResult<BusinessTripExpense> GetBusinessTripExpense(string id)
{
return Lookup(id);
}
// PATCH tables/BusinessTripExpense/48D68C86-6EA6-4C25-AA33-223FC9A27959
public Task<BusinessTripExpense> PatchBusinessTripExpense(string id, Delta<BusinessTripExpense> patch)
{
return UpdateAsync(id, patch);
}
// POST tables/BusinessTripExpense
public async Task<IHttpActionResult> PostBusinessTripExpense(BusinessTripExpense item)
{
BusinessTripExpense current = await InsertAsync(item);
return CreatedAtRoute("Tables", new { id = current.Id }, current);
}
// DELETE tables/BusinessTripExpense/48D68C86-6EA6-4C25-AA33-223FC9A27959
public Task DeleteBusinessTripExpense(string id)
{
return DeleteAsync(id);
}
}
加入 Migration
Add-Migration BusinessTripExpense
發佈到 Azure 雲端上
PostMan 測試資料
本地端
Azure
這個練習範例專案,請參考