Greg Gum
Greg Gum

Reputation: 38069

How to include only a single property when using 'expand'

I have an orders table which I want to query. One of the properties is "UserId" and which turns into a navigation property to the User.

I can query this to get the order and the associated User. However, I don't want the entire User entity, only the UserName property.

How do I construct that query in breeze?

Something like:

let query = new breeze.EntityQuery()
            .from("orders")
            .expand("user.userName");

I tried this, but then returned objects are not actually entities, but it does return just the userName:

let query = new breeze.EntityQuery()
            .from("cases")
            .select("field1, field2, user.userName");

Is there any other way of doing this? Note that I am using EF on the backside.

Upvotes: 1

Views: 57

Answers (1)

Greg Gum
Greg Gum

Reputation: 38069

The best solution I found for this is creating a "virtual entity" which consists of just the two fields that I want. In other words, I have AspNetUsers as one table with it's corresponding entity. And then I have a second code first entity which consists of only the Id, and the UserName:

[Table("AspNetUsers")]
public partial class User
{
    [Key]
    [Column("Id")]
    public string AspNetUserId { get; set; }

    [StringLength(256)]
    public string UserName { get; set; }
}

I can then use it as a relation in other tables and include it like was just another table.

Upvotes: 1

Related Questions