实体框架无法在Web.config中找到连接字符串 [英] Entity Framework Can't Find Connection String in Web.config

查看:134
本文介绍了实体框架无法在Web.config中找到连接字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

实体框架似乎并没有从Web.config中读取连接字符串。



我开始了一个新项目并创建了一个上下文:

  public class FooContext:DbContext 
{
public FooContext():
base(Foo)
{
}

// DbSets here
}

然后,我向项目Web.config添加了一个连接字符串:

 < configuration> 
< configSections>
<! - 有关实体框架配置的更多信息,请访问http://go.microsoft.com/fwlink/?LinkID=237468 - >
< section name =entityFrameworktype =System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection,EntityFramework,Version = 6.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089requirePermission =false/> ;
< / configSections>
< connectionStrings>
< add name =FooproviderName =System.Data.SqlClientconnectionString =Data Source = Foo; Initial Catalog = Foo; Integrated Security = False; User Id = foo; Password = foo; MultipleActiveResultSets =真/>
< / connectionStrings>
< appSettings>
...

我启用迁移,生成初始迁移,然后尝试更新数据库。过了一会儿,更新失败,表示它无法连接到数据库。所以我把我的项目DLL拖到LINQPad中,并运行以下操作:

  var context = new FooContext(); 
context.Database.Connection.ConnectionString.Dump();

我得到以下输出:

  Data Source = .\SQLEXPRESS; Initial Catalog = Foo; Integrated Security = True; MultipleActiveResultSets = True 

它试图连接到LocalDB,完全忽略我的连接字符串。所以我尝试在上下文构造函数中更加明确,通过使用name = Foo而不是仅仅Foo

  public FooContext():
base(name = Foo)
{
}

对于什么是值得的,我从来没有这样做过。我甚至在同一个解决方案中提供了其他项目,我刚刚传递了连接字符串名称,并且它们已经正常工作。



我跳回到LINQPad并再次运行代码,现在我得到一个例外:

 没有可以找到名为Foo的连接字符串应用配置文件。 

我完全失去了。我已经建立了这样的100多次的项目,从来没有任何问题。因为这可能很重要,我正在运行最新的实体框架6.1.3。任何想法可能在这里发生什么?

解决方案

我假设你在Visual studio中运行,确保你是运行您的Web项目作为启动项目使用web.config。如果您正在运行另一个控制台或.tests项目作为启动,它会将其app.config文件作为配置文件。


Entity Framework doesn't seem to actually be reading connection strings from the Web.config.

I started a new project and created a context:

public class FooContext : DbContext
{
    public FooContext() :
        base("Foo")
    {
    }

    // DbSets here
}

Then, I added a connection string to the projects Web.config:

<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=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <connectionStrings>
    <add name="Foo" providerName="System.Data.SqlClient" connectionString="Data Source=Foo;Initial Catalog=Foo;Integrated Security=False;User Id=foo;Password=foo;MultipleActiveResultSets=True" />
  </connectionStrings>
  <appSettings>
      ...

I enabled migrations, generated an initial migration, and then attempted to update the database. After a while, the update fails saying that it couldn't connect to the database. So I pulled my project DLL into LINQPad and ran the following:

var context = new FooContext();
context.Database.Connection.ConnectionString.Dump();

And I get the following output:

Data Source=.\SQLEXPRESS;Initial Catalog=Foo;Integrated Security=True;MultipleActiveResultSets=True

It's trying to connect to LocalDB, completely ignoring my connection string. So I tried being more explicit in the context constructor, by using "name=Foo" instead of just "Foo".

public FooContext() :
    base("name=Foo")
{
}

For what it's worth, I've never had to do this before. I've even got other projects in the same solution where the I've simply passed the connection string name and they've been working fine.

I jump back over to LINQPad and run the code again, and now I get an exception:

No connection string named 'Foo' could be found in the application config file.

I'm at a complete loss. I've set up projects like this 100s of times and never had any issues. Since it may be important, I'm running the latest Entity Framework, 6.1.3. Any ideas what could possibly be going on here?

解决方案

I am assuming you are running this in Visual studio, make sure you are running your web project as a startup project to use web.config. If you are running another console or .tests project as startup, it will pick up their app.config files as config file.

这篇关于实体框架无法在Web.config中找到连接字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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