2016-12-06 64 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

相关问题