微风投影:选择非标量导航属性时出错 [英] breeze projection : error selecting non scalar navigation properties
问题描述
public partial class Articoli {
public decimal Id_Articolo {get;组; }
public string Codice {get;组; }
public virtual ICollection< Udc_Dettaglio> FK_Udc_Dettaglio_Articoli {get;组; }
}
public partial class Udc_Dettaglio {
public decimal Id_Udc {get;组; }
public decimal Id_Articolo {get;组; }
public virtual Articoli FK_Udc_Dettaglio_Articoli {get;组; }
}
public Udc_DettaglioMap(){
//主键
this.HasKey(t => new {t.Id_Udc,t.Id_Articolo}) ;
//关系
this.HasRequired(t => t.FK_Udc_Dettaglio_Articoli)
.WithMany(t => t.FK_Udc_Dettaglio_Articoli)
.HasForeignKey(d = d.Id_Articolo);
}
如果通过使用微风,我尝试执行此查询
breeze.EntityQuery.from(Articoli)。expand(FK_Udc_Dettaglio_Articoli)
pre>
或这一个
breeze.EntityQuery.from( Articoli)选择(Codice,FK_Udc_Dettaglio_Articoli)
一切正常,但如果我尝试这个
breeze.EntityQuery.from(Articoli)。select(Codice,FK_Udc_Dettaglio_Articoli.Id_Udc)
它抛出一个exeption:
无法找到属性 Id_Udc'类型'System.Collections.Generic.ICollection`1 [AWM.Models.Udc_Dettaglio]'。
我在EF模型定义中缺少某些东西?不是因为扩展实用程序正在工作...
解决方案我相信您正在尝试构建EF不支持的查询(和OData查询语法d也不支持。
您的
FK_Udc_Dettaglio_Articoli
是Articoli的集合导航属性
。通过尝试在该集合中的每个元素的Id_Udc
属性后,您实际上正在尝试在投影中创建投影而EF不支持我所知道的。
要验证,尝试在服务器端构建一个执行所需操作的LINQ查询。像
dbContext.Articoli.Select(
a => new {a.Codice,a.FK_Udc_Dettaglio_Articoli.Select (f => new {f.Id_Udc}));
您将很快发现该查询无法编译。您会收到一条消息,如匿名类型投影初始化程序应该是简单的名称或成员访问扩展。
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 ...
解决方案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 ofArticoli
. By trying to go after just theId_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".
这篇关于微风投影:选择非标量导航属性时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!