The context cannot be used while the model is being created

前端 未结 14 1341
渐次进展
渐次进展 2020-12-03 13:27

In my application I receive the following error:

The context cannot be used while the model is being created.

I\'m not sure what

相关标签:
14条回答
  • 2020-12-03 14:02

    I also came across same issue today.
    In my case restarting Visual Studio resolved the issue.

    0 讨论(0)
  • 2020-12-03 14:03

    I have experienced this issue in the past and usually it was due not using the latest version + referencing issue.

    Try and get the newest EF version from NuGet for all your projects and see if the error goes away:
    http://blogs.msdn.com/b/adonet/archive/2012/02/09/ef-4-3-released.aspx

    UPDATE
    Another reason for this error can be that while you create the context the first time and therefore cause the model to be created you create another context on a separate thread. You will have to wait for other context instances to be created after the model creation has completed.

    0 讨论(0)
  • 2020-12-03 14:07

    I had same problem. I checked my Connection String and made sure SQL Server Service is running to resolve this issue.

    0 讨论(0)
  • 2020-12-03 14:07

    I also had the same issue... However, My problem was that I was using a custom role provider. This role provider had a reference to my context that remained open the entire lifespan of the application. This wasn't an issue until multiple web browsers were attempting to make requests at the same time.

    My solution was to create a new context in the custom RoleProvider in each overridden method and dispose via the using statement.

    using(var ctx = new Entities())
    {
        //do stuff                
    }
    

    So, basically if you are getting this error you need to look at your stack trace and see which method / file the error is occurring on and then modify the offending method / class to dispose of the context each time.

    In my case I use Ninject DI and have implemented DI on my custom role provider and also in a custom actionfilter. To fix the issue of multiple threads accessing the same context I scrapped the DI in these classes and when straight to creating a new context for each class.

    0 讨论(0)
  • 2020-12-03 14:11

    I was able to resolve this issue by adding

    MultipleActiveResultSets=true
    

    to the my EF connection string.

    I fixed it by adding this multiple thread connection parameter.

    0 讨论(0)
  • 2020-12-03 14:12

    1 - Ensure that the Connection String has a backslash instead of a forward slash:

    connectionString="Data Source=.\SQLEXPRESS ....
    

    2 - Add MultipleActiveResultSets=true to the connection string.

    3 - What was causing it for me was that I had multiple constructors to my DbContext class:

    using System.Data.Entity;
    using System.Data.Entity.Infrastructure;
    using System.Data.Entity.ModelConfiguration.Conventions;
    
    namespace MyProject
    {
        public partial class Model1 : DbContext
        {
            // I had to comment out this one ...
            //public Model1()
            //    : base("name=Model1")
            //{
            //}
    
            public Model1(bool enableLazyLoading = true)
                : base("name=Model1")
            {
                //Database.SetInitializer<Model1>(new CreateDatabaseIfNotExists<Model1>());            
                //this.Configuration.LazyLoadingEnabled = false;
    
                Database.SetInitializer<Model1>(null);
                this.Configuration.ProxyCreationEnabled = false;
    
                ((IObjectContextAdapter)this).ObjectContext.ContextOptions.LazyLoadingEnabled = enableLazyLoading;          
                ((IObjectContextAdapter)this).ObjectContext.ContextOptions.ProxyCreationEnabled = enableLazyLoading;
            }
    
            public virtual DbSet<Product> Products { get; set; }
            // ...
            protected override void OnModelCreating(DbModelBuilder modelBuilder)
            {
                // ...
            }
        }
    }
    

    Once I commented out the original Model1 constructor it solved it.

    I left 2 versions of the Lazy Loading/Database.SetInitializer to show there are 2 ways to do it - either enabled or disabled.

    • If you use the first 2 lines to NOT use it, don't use the next 4 and remove bool enableLazyLoading = true.
    • If you DO use the 4 lines like shown, ensure you keep those 2 lines commented out and keep the bool enableLazyLoading = true.
    0 讨论(0)
提交回复
热议问题