While the model is being built, the context cannot be utilized.

c# entity-framework


I have the following problem in my application:

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

I'm unsure of what this implies. I followed all the regular procedures, but this time it didn't work. Here's my code:


    <add name="DatabaseContext" connectionString="Data Source=./SQLEXPRESS;Initial Catalog=ProjectCode;Integrated Security=SSPI;" providerName="System.Data.SqlClient" />


class Products
    public int ProductID { get; set; }
    public string ProductName { get; set; }


class DatabaseContext : DbContext
    public DbSet<Products> Products { get; set; }


DatabaseContext context = new DatabaseContext();

   var products = context.Products.ToList();

   foreach (var item in products)
      Console.WriteLine(item.ProductID + " : " + item.ProductName);

The line that breaks down isvar products = context.Products.ToList();

Any suggestions as to what may be causing this? My database has 2 goods set up, therefore it ought to be producing them.


Here is the whole of my App.config file:

<?xml version="1.0" encoding="utf-8"?>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
    <add name="DatabaseContext" connectionString="Data Source=./SQLEXPRESS;Initial Catalog=ProjectCode;Integrated Security=SSPI;MultipleActiveResultSets=true" providerName="System.Data.SqlClient" />
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework">
        <parameter value="Data Source=.\SQLEXPRESS; Integrated Security=True; MultipleActiveResultSets=True" />
3/17/2012 2:23:09 PM

Accepted Answer

you have an app. Config file has a forward slash (./SQLEXPRESS) beneath connectionstrings. Make this a backslash instead. As per SQLExpress:

<add name="DatabaseContext" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=ProjectCode;Integrated Security=SSPI;" providerName="System.Data.SqlClient" />
3/17/2012 5:28:06 PM

Popular Answer

I've had similar problem in the past, and it was often caused by using an older version and having a referring problem.

See whether the problem disappears by attempting to download the most recent EF version from NuGet for all your projects:

Another possible explanation for this problem is that when you first construct the context and thus cause the model to be formed, you also build another context on a different thread. After the model is built, you will need to wait until new context instances are produced.

Related Questions


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