简体   繁体   中英

breezejs SubQuery one-to-many relationship

I am using AngularJS/Breeze and I need to write the ( http://www.breezejs.com/documentation/query-examples ) following SQL using Breeze format:

SELECT * FROM CONTACT WHERE ID IN (SELECT contact_id from pharmacy_contact where pharmacy_id='11855'.

My model classes are as follows:

[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; }

}

Please let me know how I should write the above query in breeze

You may want to rephrase your SQL statement as follows:

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

You can achieve that in breeze:

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

Given that you modify the contact class to include its 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; }

}

EDIT

Also the pharmacy_contact class to include the navigation back to 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; }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM