EntityFramework Code首先,是否可以映射它

时间:2011-04-29 08:53:58

标签: ef-code-first

是否有可能将此映射到一个实体?

select x,y,z, (select count(*) from othertable where tableid=table.id) as othertablecount
from table t

我想把它映射到一个看起来像这样的类:

public class Stuff
{
    public string x { get; set; }
    public string y { get; set; }
    public string z { get; set; }
    public int count { get; set; }
}

1 个答案:

答案 0 :(得分:0)

没有。您应该将其正确映射为集合并使用投影进行查询:

class Stuff
{
   ...
   public virtual ICollection<OtherStuff> { get; set; }
}

var stuffWithCount = from stuff in myContext.Stuff
                     select new
                            {
                                stuff.x, ...
                                count = stuff.OtherStuff.Count()
                            };