c# - LINQ 和 EF6 : The context cannot be used while the model is being created

标签 c# asp.net-mvc linq entity-framework entity-framework-6

我有一个带 EF6 的 MVC 5 应用程序,我在 SignalR Hub 中尝试了我的第一个 LINQ 查询。 它在我单击测试 View 页面上的按钮时运行。

但我在查询时遇到异常:

LINQ fail

此查询是我在 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 : The context cannot be used while the model is being created,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23292224/

相关文章:

c# - 我使该方法返回什么类型?

c# - 从上下文菜单项事件处理程序获取树节点

jquery - 单点登录 ASP.NET MVC

c# - 使用 RouteArea 后 ASP.NET MVC 5 路由停止工作

c# - 为什么 XDocument.Load() 和 XmlDocument.LoadXml() 无法加载注释包含两个以上 "--"符号的 XML?

c# - 如何在一个项目中使用两个不同的 Microsoft Interop 程序集?

c# - 内容处置:ASP.Net 中的内联 PDF 文件不起作用

c# - 调用另一个 Controller 的 Action 并将 View 作为字符串

c# - 使用未知类型动态构建 LINQ 表达式

C# if 语句在 LINQ select 子句中