我有一个带有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;

关于c# - LINQ和EF6:创建模型时无法使用上下文,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23292224/

10-12 16:11