我有一个使用EF6的MVC 5应用程序,我在SignalR Hub中尝试了我的第一个LINQ查询。 它在我单击测试视图页面上的按钮时运行。
但我在查询中得到一个例外:
此查询是我在MVC5模板中的第一个单独的代码操作,模板具有单独的用户帐户。我之前只创建了模型类。
如果有帮助:我使用的上下文是模板ApplicationDbContext:IdentityContext<ApplicationUser>
我的&#34; OnModelCreating&#34;方法如下所示:
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);}
提前谢谢!
答案 0 :(得分:0)
你可以尝试一下,看看是否有帮助
int result = -1;
using (var context = new ApplicationDbContext())
{
var battles = context.Battles.Single(o =>o.BattleId == 1);
result = battles.Round;
}
return result;
答案 1 :(得分:0)
我遇到了与EF6.1.1完全相同的问题,我对这个问题的解决方案非常简单......
变化:
protected ApplicationDbContext db = new ApplicationDbContext();
要:
protected ApplicationDbContext Db {get; set;}
public TestHub()
{
Db = new ApplicationDbContext();
}
但如果你不喜欢这种方式,你也可以使用LazySingleton Pattern方法。
快乐的编码!