被创建的模型,而上下文不能使用 [英] The context cannot be used while the model is being created

查看:91
本文介绍了被创建的模型,而上下文不能使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用我收到以下错误:


  

正在创建的模型,而该上下文不能被使用。


我不知道这意味着什么。我所做的一切都是正常的,通常它的工作原理,但为这一个它不是。下面是我的code:

App.config中:

<&是connectionStrings GT;
    <添加名称=DatabaseContext的connectionString =数据源= / SQLEX $ P $干燥综合征;初始目录=项目code;集成安全性= SSPI;的providerName =System.Data.SqlClient的/>
 < /&是connectionStrings GT;

Products.cs:

 类产品
{
    公众诠释的ProductID {搞定;组; }
    公共字符串产品名称{搞定;组; }
}

DatabaseContext.cs:

 类DatabaseContext:的DbContext
{
    公共DbSet<产品>产品{搞定;组; }
}

Program.cs的:

  DatabaseContext背景=新DatabaseContext();尝试
{
   变种产品= context.Products.ToList();   的foreach(在产品VAR项)
   {
      Console.WriteLine(item.ProductID +:+ item.ProductName);
   }
      到Console.ReadLine();
}

该生产线是对失败的 VAR产品= context.Products.ToList();

任何想法可能会导致什么呢?我已成立了2款产品在我的数据库,所以它应该是输出他们。

修改

下面是我的整个App.config文件:

 <?XML版本=1.0编码=UTF-8&GT?;
<结构>
  < configSections>
    <! - 有关Entity Framework的配置的详细信息,请访问http://go.microsoft.com/fwlink/?LinkID=237468 - >
    <节名称=的EntityFrameworkTYPE =System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection,的EntityFramework,版本4.3.1.0 =文化=中性公钥= b77a5c561934e089/>
  < / configSections>
  <&是connectionStrings GT;
    <添加名称=DatabaseContext的connectionString =数据源= / SQLEX $ P $干燥综合征;初始目录=项目code;集成安全性= SSPI; MultipleActiveResultSets =真的providerName =System.Data.SqlClient的/ >
  < /&是connectionStrings GT;
  <&启动GT;
    < supportedRuntime版本=V4.0SKU =.net框架,版本= V4.5/>
  < /启动>
  <&的EntityFramework GT;
    < defaultConnectionFactory TYPE =System.Data.Entity.Infrastructure.SqlConnectionFactory,的EntityFramework>
      <&参数GT;
        <参数值=数据源= \\ SQLEX $ P $干燥综合征;集成安全性= TRUE; MultipleActiveResultSets = TRUE/>
      < /参数>
    < / defaultConnectionFactory>
  < /&的EntityFramework GT;
< /结构>


解决方案

在您的app.config文件下的ConnectionStrings你有一个正斜杠(./SQLEX$p$pSS)。更改为反斜杠\\ SQLEX $ P $干燥综合征,像这样:

 <添加名称=DatabaseContext的connectionString =数据源= \\ SQLEX $ P $干燥综合征;初始目录=项目code;集成安全性= SSPI;的providerName =System.Data.SqlClient的/>

In my application I receive the following error:

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

I'm not sure what this means. I have done everything as normal and usually it works but for this one it isnt. Below is 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 is fails on is var products = context.Products.ToList();

Any ideas what could be causing this? I have set up 2 products in my database so it should be outputting them.

EDIT

Here is my whole 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>

解决方案

In your App.Config file under connectionstrings you had a forward slash (./SQLEXPRESS). Change this to a backslash .\SQLEXPRESS like so:

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

这篇关于被创建的模型,而上下文不能使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆