C# 实体框架DbContext负载性能
我有以下模型类和关联的DbContext对象C# 实体框架DbContext负载性能,c#,sql-server,asp.net-mvc,entity-framework,C#,Sql Server,Asp.net Mvc,Entity Framework,我有以下模型类和关联的DbContext对象 public class PlayerModel { [Key] public string PlayerID { get; set; } [InverseProperty("Player")] public virtual ICollection<SeasonStats> SeasonStats { get; set; } //General public string FirstNa
public class PlayerModel
{
[Key]
public string PlayerID { get; set; }
[InverseProperty("Player")]
public virtual ICollection<SeasonStats> SeasonStats { get; set; }
//General
public string FirstName { get; set; }
public string LastName { get; set; }
public int Height { get; set; }
public int Weight { get; set; }
public PlayerModel()
{
this.SeasonStats = new List<SeasonStats>();
}
}
public class PlayerModelDBContext : DbContext
{
public virtual DbSet<PlayerModel> Players { get; set; }
}
请求正在超时,因为我预计填充DbContext的时间太长。如何将上下文配置为仅在请求时从数据库获取记录?您是否尝试返回
视图(DB.Players.AsQueryable().Take(20))
?据我所知,这个方法是优化发送到数据库的查询。一个7500个条目的请求不应该超时。但即便如此,你的代码也在做你想让它做的事情。显然其他地方出了问题。将加载7500个条目,更不用说排名前20位的条目了。我的猜测是,您的视图对resultset做了一些不好的事情。您希望它超时,但它真的超时了吗?尝试使用EF探查器进行验证。或者您可以调试它,试试看db.Players.Take(20).ToList()
是否真的需要那么长的时间。这应该不是问题,但是这里的virtual关键字有什么意义:public virtual DbSet Players{get;set;}?
public ViewResult Index()
{
return View(db.Players.Take(20));
}