BreezeJs - 使用expand()时,两个外键都涉及到一个共同的实体

问题描述:

说,我有两个实体,Movement (id, #fromLocationId, #toLocationId)fromLocationId和第二实体Location (id, name)toLocationId是两个外键。我想写一个Breeze查询,它从位置ID和到位置ID检索位置名称与位置名称相关的所有位移名称相关的。这是我迄今:BreezeJs - 使用expand()时,两个外键都涉及到一个共同的实体

var query = breeze.EntityQuery('movement').expand('location');

当我调试和检查的第一条记录,例如,我发现它有一个位置()LOCATION1()属性。我可以从位置Iddata[0].location().name()检索位置名称,但不能与位置ID相同,因为location1()为空。我甚至试过var query = breeze.EntityQuery('movement').expand('location, location1');,但它仍然无法正常工作。

关于如何解决这个问题的任何想法?提前致谢。

编辑

以下是.NET类:

[Table("Location")] 
public partial class Location 
{ 
    public Location() 
    { 
     Movements = new HashSet<Movement>(); 
     Movements1 = new HashSet<Movement>(); 
    } 

    public int Id { get; set; } 

    [StringLength(250)] 
    public string Name { get; set; } 

    public virtual ICollection<Movement> Movements { get; set; } 

    public virtual ICollection<Movement> Movements1 { get; set; } 
} 

[Table("Movement")] 
public partial class Movement 
{ 
    public int Id { get; set; } 

    public int FromLocationId { get; set; } 

    public int ToLocationId { get; set; } 

    public virtual Location Location { get; set; } 

    public virtual Location Location1 { get; set; } 
} 

在的DbContext类,关系是这样的:

 modelBuilder.Entity<Location>() 
      .HasMany(e => e.Movements) 
      .WithRequired(e => e.Location) 
      .HasForeignKey(e => e.FromLocationId) 
      .WillCascadeOnDelete(false); 

     modelBuilder.Entity<Location>() 
      .HasMany(e => e.Movements1) 
      .WithRequired(e => e.Location1) 
      .HasForeignKey(e => e.ToLocationId) 
      .WillCascadeOnDelete(false); 

感谢。

好的,我发现我需要做什么。这里是解决方案:

var query = breeze.EntityQuery('movement').expand('location, location')

我想,微风明白这一点为,第一位置expand()将与fromLocationId和第二位置将与toLocationId 。换句话说,将与该公共实体相关的外键添加到许多entity

希望它可以帮助别人。

+0

我不这么认为。其他事情正在发生。服务器上导航属性的名称是什么? “运动”类型的元数据是什么(它真的叫做这个还是叫做“运动”?)? – Ward 2015-02-20 01:49:22

+0

正如问题所述,实体是“运动”和“位置”(与服务器上的一样)。 'Location'的属性是* Id *和* Name *,'Movement'的属性是* Id *,* FromLocationId *和* ToLocationId *。属性* FromLocationId *和* ToLocationId *都是指向'Location'的外键。你似乎是对的,因为BreezeJs没有检索与这两个外键相对应的所有名字。我该怎么办? – RipHamilton 2015-02-21 09:19:49

+0

你没有描述你的后端技术。你使用EF吗? – Ward 2015-02-23 21:46:52