ef cf linq 填充忽略的属性
我确信以前已经问过这个问题,但我找不到一个好的方法来搜索它。
我有一个像下面这样的类
public class Vendor
{
public int VendorId { get; set; }
public string Name { get; set; }
public int ProductCount { get; set; }
}
我有一个像下面这样的配置类设置
public class VendorConfiguration : EntityTypeConfiguration<Vendor>
{
public VendorConfiguration()
{
Property(p => p.Name).IsRequired().HasMaxLength(128);
Ignore(v => v.ProductCount);
}
}
这是我用来获取供应商的查询。
public Vendor[] GetVendors()
{
using (var db = new UbidContext())
{
var query = (from vendor in db.Vendors
select vendor);
return query.ToArray();
}
}
我如何使用看起来类似于的子查询填充 ProductCount
ProductCount = (from vend in db.VendorProducts
where vend.VendorId == id
select vend).Count()
有没有办法将其添加到主查询中,这样我只对数据库进行 1 次调用?
谢谢, 安德鲁
I'm sure this has to have been asked before, but I couldn't find a good way to search on it.
I have a class like the following
public class Vendor
{
public int VendorId { get; set; }
public string Name { get; set; }
public int ProductCount { get; set; }
}
I have a configuration class setup like
public class VendorConfiguration : EntityTypeConfiguration<Vendor>
{
public VendorConfiguration()
{
Property(p => p.Name).IsRequired().HasMaxLength(128);
Ignore(v => v.ProductCount);
}
}
Here is the query i use to grab the vendors.
public Vendor[] GetVendors()
{
using (var db = new UbidContext())
{
var query = (from vendor in db.Vendors
select vendor);
return query.ToArray();
}
}
How could I populate ProductCount with a subquery that would look similar to
ProductCount = (from vend in db.VendorProducts
where vend.VendorId == id
select vend).Count()
Is there a way I can add that to the main query, so I'm only making 1 call to the db?
Thanks,
Andrew
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我会这样尝试:
问题是您必须投影到非实体类型(在上面的示例中为匿名),然后将投影的 ProductCount 值复制到投影的 Vendor 中> 退货前逐项进行。
I would try it this way:
Problem is that you must project into a non-entity type (anonymous in the example above) and then copy the projected
ProductCount
value into the projectedVendor
item by item before you return it.