我需要一个新属性,在哪里统计相关的注册实体。所以我已经定义了一个未映射的属性来获得免费席位。
public class Offer
{
public int OfferID { get; set;}
public int maxRegistration { get; set;}
public virtual ICollection<Registration> Registrations { get; set; }
[NotMapped]
public int FreeSeats
{
get
{
int registrationCounts = Registrations.Count();
return maxRegistration - registrationCounts;
}
}
}
这给我带来了错误:
There is already an open DataReader associated with this Command which must be closed first
所以,我重写代码:
MyApp.Context db = new MyApp.Context();
int registrationCounts = db.Registration.Where(r => r.OfferID == OfferID).Count();
这是正常的,但速度很慢 是否有更优雅的方法来计算数据集的相关实体?