breeze projection : error selecting non scalar nav

2019-05-22 14:33发布

问题:

Ho to everybody. Below you can see the partial result of the EF Power Tools reverse engineering made on the database of mine.

public partial class Articoli {        
    public decimal Id_Articolo { get; set; }
    public string Codice { get; set; }
    public virtual ICollection<Udc_Dettaglio> FK_Udc_Dettaglio_Articoli { get; set; }
}

public partial class Udc_Dettaglio {
    public decimal Id_Udc { get; set; }
    public decimal Id_Articolo { get; set; }
    public virtual Articoli FK_Udc_Dettaglio_Articoli { get; set; }
}

public Udc_DettaglioMap() {
    // Primary Key
    this.HasKey(t => new { t.Id_Udc, t.Id_Articolo });

    // Relationships
    this.HasRequired(t => t.FK_Udc_Dettaglio_Articoli)
        .WithMany(t => t.FK_Udc_Dettaglio_Articoli)
        .HasForeignKey(d => d.Id_Articolo);        
}

If, by using breeze, i try to perform this query

breeze.EntityQuery.from("Articoli").expand("FK_Udc_Dettaglio_Articoli")

or this one

breeze.EntityQuery.from("Articoli").select("Codice,FK_Udc_Dettaglio_Articoli")

everything works fine, but if i try with this

breeze.EntityQuery.from("Articoli").select("Codice,FK_Udc_Dettaglio_Articoli.Id_Udc")

it throw an exeption :

Unable to locate property 'Id_Udc' on type 'System.Collections.Generic.ICollection`1[AWM.Models.Udc_Dettaglio]'."

Am I missing something in EF model definition ? i guess not because expand utility is working ...

回答1:

I believe you are trying to build a query that EF doesn't support (and that OData query syntax doesn't support either).

Your FK_Udc_Dettaglio_Articoli is a collection navigation property of Articoli. By trying to go after just the Id_Udc property of each element in that collection, you are actually trying to create a projection within a projection ... and EF doesn't support that as far as I know.

To verify, try to construct a LINQ query on the server side that does what you want. Something like

dbContext.Articoli.Select(
    a => new {a.Codice, a.FK_Udc_Dettaglio_Articoli.Select(f => new {f.Id_Udc}));

You'll quickly discover that that query does not compile. You get a message something like "anonymous type projection initializer should be simple name or member access expansion".