LINQ and EF6: The context cannot be used while the model is being created

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

Question

I have an MVC 5 Application with EF6, and I tried my first LINQ query in a SignalR Hub. It runs when I click on a button on a Test view page.

But I get an exception at the query:

LINQ fail

This query is my first individual code action in the MVC5 template with individual User Accounts by the template. I only created the model classes before.

If it helps: The context I'm using is the template ApplicationDbContext:IdentityContext<ApplicationUser>

My "OnModelCreating" method looks like this:

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);}

Thank you in advance!

1
2
4/25/2014 12:05:13 PM

Popular Answer

I had the exact same problem with EF6.1.1 and my solution to the problem was quite simple...

Change:

protected ApplicationDbContext db = new ApplicationDbContext();

To:

protected ApplicationDbContext Db {get; set;}

public TestHub()
{
  Db = new ApplicationDbContext();
}

But if you don't like this way, you can also use the LazySingleton Pattern approach.

Happy coding!

0
7/5/2014 8:56:59 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow