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

c# entity-framework

Question

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:

App.config:

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

Products.cs:

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

DatabaseContext.cs:

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

Program.cs:

DatabaseContext context = new DatabaseContext();

try
{
   var products = context.Products.ToList();

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

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.

EDIT

Here is the whole of my App.config file:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <!-- 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=4.3.1.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
  </configSections>
  <connectionStrings>
    <add name="DatabaseContext" connectionString="Data Source=./SQLEXPRESS;Initial Catalog=ProjectCode;Integrated Security=SSPI;MultipleActiveResultSets=true" providerName="System.Data.SqlClient" />
  </connectionStrings>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="Data Source=.\SQLEXPRESS; Integrated Security=True; MultipleActiveResultSets=True" />
      </parameters>
    </defaultConnectionFactory>
  </entityFramework>
</configuration>
1
27
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" />
17
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:
http://blogs.msdn.com/b/adonet/archive/2012/02/09/ef-4-3-released.aspx

UPDATE
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





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