![](/img/trans.png)
[英]EF + async = The context cannot be used while the model is being created?
[英]LINQ and EF6: The context cannot be used while the model is being created
我有一个带有EF6的MVC 5应用程序,我在SignalR集线器中尝试了我的第一个LINQ查询。 当我单击“测试”视图页面上的按钮时,它将运行。
但是我在查询中遇到异常:
该查询是我在MVC5模板中的第一个单独的代码操作,其中包含模板中的各个用户帐户。 我以前只是创建模型类。
如果有帮助:我正在使用的上下文是模板ApplicationDbContext:IdentityContext<ApplicationUser>
我的“ OnModelCreating”方法如下所示:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
modelBuilder.Entity<Player>()
.HasMany(m => m.MilitaryAccess)
.WithMany();
modelBuilder.Entity<Player>()
.HasMany(m => m.FactionRelationship)
.WithMany();
base.OnModelCreating(modelBuilder);}
先感谢您!
您可以尝试一下,看看是否有帮助
int result = -1;
using (var context = new ApplicationDbContext())
{
var battles = context.Battles.Single(o =>o.BattleId == 1);
result = battles.Round;
}
return result;
我在EF6.1.1中遇到了完全相同的问题,而我对该问题的解决方案非常简单...
更改:
protected ApplicationDbContext db = new ApplicationDbContext();
至:
protected ApplicationDbContext Db {get; set;}
public TestHub()
{
Db = new ApplicationDbContext();
}
但是,如果您不喜欢这种方式,也可以使用LazySingleton模式方法。
编码愉快!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.