2014-07-26 29 views
0

我使用AngularJS /微風,我需要用微風格式寫的(http://www.breezejs.com/documentation/query-examples)以下SQL:breezejs子查詢一個一對多的關係

SELECT * FROM聯繫WHERE ID IN(從pharmacy_contact其中pharmacy_id選擇CONTACT_ID =「11855」

我的模型類如下:

[Table("pharmacy_contact")] 
public class PharmacyContact 
{ 
    [Key] 
    public int Id { get; set; } 
    public int? PharmacyId { get; set; } 
    public int? ContactId { get; set; } 

} 

[Table("contact")] 
public class Contact 
{ 
    [Key] 
    public int Id { get; set; } 
    public string First_Name { get; set; } 
    public string Last_Name { get; set; } 
    public string Title { get; set; } 
    public string Phone { get; set; } 
    public string Extn { get; set; } 
    public string Fax { get; set; } 
    public string Email { get; set; } 
    public string Contact_Daytime { get; set; } 
    public string Contact_Method { get; set; } 
    public string Contact_Type { get; set; } 

} 

請讓我知道我應該怎麼寫上面的查詢在微風

回答

2

您可能需要修改你的SQL語句如下:

SELECT * FROM CONTACT inner join pharmacy_contact on ID = contact_id where pharmacy_id=11855. 

可以實現,在微風:

var query = EntityQuery.from('CONTACTS') 
    .expand('pharmacy_contacts') 
    .where('pharmacy_contacts','any','pharmacy_id', '==', 11855); 

既然你修改contact類包括其pharmacy_contacts:

[Table("contact")] 
public class Contact 
{ 
    [Key] 
    public int Id { get; set; } 
    public string First_Name { get; set; } 
    public string Last_Name { get; set; } 
    public string Title { get; set; } 
    public string Phone { get; set; } 
    public string Extn { get; set; } 
    public string Fax { get; set; } 
    public string Email { get; set; } 
    public string Contact_Daytime { get; set; } 
    public string Contact_Method { get; set; } 
    public string Contact_Type { get; set; } 
    public ICollection<pharmacy_contact> pharmacy_contacts { get; set; } 

} 

編輯

另外,pharmacy_contact類包括導航回聯繫

[Table("pharmacy_contact")] 
public class PharmacyContact 
{ 
    [Key] 
    public int Id { get; set; } 
    public int? PharmacyId { get; set; } 
    [ForeignKey("Contact")] 
    public int? ContactId { get; set; } 
    public contact contact { get; set; } 
} 
+0

我已經實現了上述的建議,但我得到一個錯誤: 「無法找到屬性:上的EntityType pharmacy_Contact:聯繫人:#AIE.Model查詢失敗「。我嘗試着遵循Angular/Breeze遵循的camelcasing慣例,但目前爲止還沒有人工作。請讓我知道我應該如何對此進行處理。 – user3153361

+0

這是因爲你需要告訴EF有關FK的信息。在'pharmacy_contact'類中,在ContactId屬性前添加註釋'[ForeignKey(「Contact」)] –

+0

添加FK後,我得到了上面提到的以下錯誤: {「類型'AIE.Model.Pharmacy_Contact'上屬性'ContactId'上的ForeignKeyAttribute無效。在依賴類型'AIE.Model.Pharmacy_Contact'上未找到導航屬性'contact'。應該是一個有效的導航屬性名稱。「} – user3153361