我有.net 4.5.2測試應用程序正在玩Azure移動服務,我嘗試使用TableController
存儲數據。我有我的數據類型如下:使用EF從Azure移動服務檢索父/子記錄(包含列表的對象)
public class Run:EntityData
{
public int RunId { get; set; }
public DateTime? ActivityStarted { get; set; }
public DateTime? ActivityCompleted { get; set; }
public List<Lap> LapInformation { get; set; }
public Run()
{
LapInformation = new List<Lap>();
}
}
public class Lap
{
[Key]
public int LapNumber { get; set; }
public int CaloriesBurnt { get; set; }
public double Distance {get; set;}
//Some other basic fields in here
public DateTime? LapActivityStarted { get; set; }
public DateTime? LapActivityCompleted { get; set; }
public Lap()
{
}
在我啓動I類撥打:
HttpConfiguration config = new HttpConfiguration();
new MobileAppConfiguration()
.UseDefaultConfiguration()
.ApplyTo(config);
在我MobileServiceContext類:
public class MobileServiceContext : DbContext
{
private const string connectionStringName = "Name=MS_TableConnectionString2";
public MobileServiceContext() : base(connectionStringName)
{
}
public DbSet<Run> Runs { get; set; }
public DbSet<Lap> Laps { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Add(
new AttributeToColumnAnnotationConvention<TableColumnAttribute, string>(
"ServiceTableColumn", (property, attributes) => attributes.Single().ColumnType.ToString()));
}
}
在我的控制器的話,我有:
[MobileAppController]
public class RunController: TableController<Run>
{
protected override void Initialize(HttpControllerContext controllerContext)
{
base.Initialize(controllerContext);
MobileServiceContext context = new MobileServiceContext();
DomainManager = new EntityDomainManager<Run>(context, Request);
}
public IList<Run> GetAllRuns()
{
var runs = context.Runs.Include("LapInformation").ToList();
return runs;
}
public SingleResult<Run> GetRun(string id)
{
return Lookup(id);
}
public async Task<IHttpActionResult> PostRun(Run run)
{
Run current = await InsertAsync(run);
return CreatedAtRoute("Tables", new { id = current.Id }, current);
}
public Task DeleteRun(string id)
{
return DeleteAsync(id);
}
}
我可以再POST
在fiddler
記錄,其與201
和新建項目的位置響應。我張貼的數據的一個例子是:
{RunId: 1234, LapInformation:[{LapNumber:1,Distance:0.8, LapActivityStarted: "2017-06-19T00:00:00", LapActivityCompleted: "2017-06-19T00:00:00", CaloriesBurnt: 12}]}
然而,當我GET
該對象,我只讓從運行的領域,沒有詳細記錄列表(LAP)。有什麼我必須在實體框架中配置,以便當我從數據庫獲取運行記錄時,它還獲取並反序列化所有關聯的詳細記錄?
希望這是有道理的。
編輯 原來,這是拉回所有的圈信息,但是當我將它返回給客戶端時,該信息就會丟失。
你解決了這個問題嗎,你需要進一步的幫助嗎? –
感謝您關注@ Bruce-MSFT。我最終採納了你的建議:「我更喜歡單獨處理表格,並且手動處理移動客戶端上的關係管理,這會在移動客戶端上產生更多的代碼,但通過避免大多數關係的複雜性,使服務器變得更簡單。」只要獨立處理父母和孩子,並繼續生活! :D – LDJ