2017-06-20 16 views
0

我有.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); 
} 

}

我可以再POSTfiddler記錄,其與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)。有什麼我必須在實體框架中配置,以便當我從數據庫獲取運行記錄時,它還獲取並反序列化所有關聯的詳細記錄?

希望這是有道理的。

編輯 原來,這是拉回所有的圈信息,但是當我將它返回給客戶端時,該信息就會丟失。

+0

你解決了這個問題嗎,你需要進一步的幫助嗎? –

+1

感謝您關注@ Bruce-MSFT。我最終採納了你的建議:「我更喜歡單獨處理表格,並且手動處理移動客戶端上的關係管理,這會在移動客戶端上產生更多的代碼,但通過避免大多數關係的複雜性,使服務器變得更簡單。」只要獨立處理父母和孩子,並繼續生活! :D – LDJ

回答

1

您可以使用自定義EF查詢與Include()方法而不是Lookup調用,最好是從System.Data.Entity命名空間中使用函數的重載。

var runs = context.Runs.Include(r => r.LapInformation) 

看看https://msdn.microsoft.com/en-us/library/jj574232(v=vs.113).aspx

+0

嗯...好的,所以包括,使用一個字符串(這是唯一的簽名,我有include()方法)帶回數據。但是,它沒有回到客戶端。這是一個序列化問題嗎? – LDJ

+0

@LDJ因此返回了什麼?首先想到的不應該有任何問題。 也可以使用其他'Include()'簽名使用'System.Data.Entity'命名空間。 –

1

據我所知,你也可以使用$expand參數擴展您的收藏如下:

GET /tables/Run$expand=LapInformation 

這裏是我的樣品,你可以參考一下吧:

enter image description here

你可能標誌着一個自定義ActionFilterAttribute你的行動如下自動添加$expand屬性爲您的查詢請求:

// GET tables/TodoItem 
[ExpandProperty("Tags")] 
public IQueryable<TodoItem> GetAllTodoItems() 
{ 
    return Query(); 
} 

有關詳細信息,你可以參考阿德里安·霍爾的書chapter3 relationships

編輯原來,它是拉回所有的圈信息,但是當我將它返回給客戶端時,該信息正在丟失。

public class TodoItem 
{ 
    public string Id { get; set; } 
    public string UserId { get; set; } 
    public string Text { get; set; } 
    public List<Tag> Tags { get; set; } 
} 

public class Tag 
{ 
    public string Id { get; set; } 
    public string TagName { get; set; } 
} 

執行後,以下拉運,我可以檢索標籤如下:

await todoTable.PullAsync("todoItems", todoTable.CreateQuery()); 

我在移動客戶端定義以下車型注意:Tags數據是隻讀的,您只能更新通知在ToDoItem表中。

此外,如阿德里安·霍爾在Data Access and Offline Sync - The Domain Manager提到:

我更喜歡單獨處理表格和手動移動客戶端上的操作關係管理。這會在移動客戶端上產生更多的代碼,但通過避免大多數關係的複雜性,使服務器變得更簡單。