2016-12-06 54 views
0

您好,我正在嘗試使用Azure移動應用中的實體框架工作來獲取關係數據。如何從azure移動應用獲取關係數據

this tutorial中的信息談論了很多關於格式化的內容,而不是關於關係。 This one具有使用實體框架的相關數據,但沒有任何關於天藍色的移動應用程序。在這種情況下,他們使用ASP.net核心。我怎樣才能把兩者的信息放在一起?

我試圖以類似的方式建立關係,但它根本不承認關係。有人知道該怎麼做嗎?

更新 以下建議我設法讓外鍵顯示在遷移中,所以。

實體的定義如下

public class Member : EntityData 
{ 
    public string FirstName { get; set; } 
    public string LastName { get; set; } 
    public string Address { get; set; } 
    public virtual ICollection<Subscription> Subscriptions { get; set; } 


} 

public class Subscription : EntityData 
{ 
    public string MemberId { get; set; } 
    public string ShopItemId { get; set; } 
    public virtual Member Member { get; set; } 
    public virtual ShopItem ShopItem { get; set; } 
    public int Quantity { get; set; } 
} 
public class ShopItem : EntityData 
{ 
    public decimal Price { get; set; } 
    public string Name { get; set; } 
    public string Description { get; set; } 
    public string Note { get; set; } 
} 

我懷疑我不需要虛擬屬性,但我會留下來,因爲在他們那裏的客戶端。

遷移顯示方面外鍵

CreateTable(
      "dbo.Subscriptions", 
      c => new 
       { 
        Id = c.String(nullable: false, maxLength: 128, 
         annotations: new Dictionary<string, AnnotationValues> 
         { 
          { 
           "ServiceTableColumn", 
           new AnnotationValues(oldValue: null, newValue: "Id") 
          }, 
         }), 
        MemberId = c.String(maxLength: 128), 
        ShopItemId = c.String(maxLength: 128), 
        Quantity = c.Int(nullable: false), 
        Version = c.Binary(nullable: false, fixedLength: true, timestamp: true, storeType: "rowversion", 
         annotations: new Dictionary<string, AnnotationValues> 
         { 
          { 
           "ServiceTableColumn", 
           new AnnotationValues(oldValue: null, newValue: "Version") 
          }, 
         }), 
        CreatedAt = c.DateTimeOffset(nullable: false, precision: 7, 
         annotations: new Dictionary<string, AnnotationValues> 
         { 
          { 
           "ServiceTableColumn", 
           new AnnotationValues(oldValue: null, newValue: "CreatedAt") 
          }, 
         }), 
        UpdatedAt = c.DateTimeOffset(precision: 7, 
         annotations: new Dictionary<string, AnnotationValues> 
         { 
          { 
           "ServiceTableColumn", 
           new AnnotationValues(oldValue: null, newValue: "UpdatedAt") 
          }, 
         }), 
        Deleted = c.Boolean(nullable: false, 
         annotations: new Dictionary<string, AnnotationValues> 
         { 
          { 
           "ServiceTableColumn", 
           new AnnotationValues(oldValue: null, newValue: "Deleted") 
          }, 
         }), 
       }) 
      .PrimaryKey(t => t.Id) 
      .ForeignKey("dbo.Members", t => t.MemberId) 
      .ForeignKey("dbo.ShopItems", t => t.ShopItemId) 
      .Index(t => t.MemberId) 
      .Index(t => t.ShopItemId) 
      .Index(t => t.CreatedAt, clustered: true); 

的正確關係但是發出請求表API時,結果仍然是

{"deleted":false,"updatedAt":"2016-12-07T21:55:19.174Z","createdAt":"2016-12-07T21:55:19.128Z","version":"AAAAAAAAB/s=","id":"1","quantity":1,"shopItemId":"1","memberId":"1"} 

不包括相關的實體。

回答

1

我也是新手,但是我發現如果您正確地命名字段,它似乎會自動檢測關係。

Item Table 
_______ 
Id 
PersonId 

Person Table 
____________ 
Id 
FirstName 
LastName 

在這裏,Item表中的PersonId將匹配Person表中的Id。

我對此很新,所以我可能會離開,但這是我對此的理解。

只需在您的模型中進行設置,然後查看遷移文件並查看是否將其提取出來。您可以通過查看遷移文件中的代碼來查看。

更新:

這裏是代碼。我沒有處理懶加載,所以我不知道如何做到這一點。

class Item 
{ 
    string Id { get; set; } 
    string PersonId { get; set; } // This matches Person class Id property 
} 

class Person 
{ 
    string Id { get; set; } 
    string FirstName { get; set; } 
    string LastName { get; set; } 
} 

另一個更新:

我只是想出了外鍵是如何工作的。

public class GameObject : EntityData 
{ 
    public string PlayerId { get; set; } 
    public virtual Player player { get; set; } 
} 

public class Player : EntityData 
{ 
    public string FirstName { get; set; } 
    public string LastName { get; set; } 
} 

虛擬屬性告訴它它是一個外鍵。沒有它,你不會看到

.ForeignKey("dbo.Players", t => t.PlayerId) 

當你的遷移創建時。

此外,要檢索相關數據,您必須按照此處列出的步驟操作:(blogs.msdn.microsoft。com/azuremobile/2014/05/27/

+0

嘿。從我的理解應該工作,這就是我所做的。兩件事情,但你能寫出答案來顯示實際的代碼嗎?只是爲了確保我不會錯過任何東西。此外,這是如何解釋延遲加載,急切加載等從我讀你必須自己處理。 – xerotolerant

+0

另外我檢查了我的migrations文件,出於某種原因,那裏確實沒有任何內容。我的數據庫有數據,我的種子方法工作正常,但是我的initialCreate遷移文件是空的,除了空的上下方法的類。 – xerotolerant

+0

我添加了顯示兩個示例模型的代碼。 – Kenny

相關問題