Winforms - 程序无法从 app.config 中找到连接字符串

Winforms - program cannot find connection string from app.config

我正在使用 VS 2013。我正在尝试将 SQL Server Compact 与 Entity Framework 一起使用 6. 在 VS 2012 中创建数据库和 .EDMX 模型并在 VS 2013 中打开。我从 NuGet 安装了最新的 EF 和 EF SQL.CE。当我尝试 运行 时,出现以下错误

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

app.config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <configSections>
        <section name="entityFramework" 
                 type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    </configSections>
    <entityFramework>
        <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlCeConnectionFactory, EntityFramework">
            <parameters>
                <parameter value="System.Data.SqlServerCe.4.0" />
            </parameters>
        </defaultConnectionFactory>
        <providers>
            <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
            <provider invariantName="System.Data.SqlServerCe.4.0" type="System.Data.Entity.SqlServerCompact.SqlCeProviderServices, EntityFramework.SqlServerCompact" />
        </providers>
    </entityFramework>
    <system.data>
        <DbProviderFactories>
            <remove invariant="System.Data.SqlServerCe.4.0" />
            <add name="Microsoft SQL Server Compact Data Provider 4.0" invariant="System.Data.SqlServerCe.4.0" description=".NET Framework Data Provider for Microsoft SQL Server Compact" type="System.Data.SqlServerCe.SqlCeProviderFactory, System.Data.SqlServerCe, Version=4.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" />
        </DbProviderFactories>
    </system.data>
    <connectionStrings>
        <add name="SomeDbFile" 
             connectionString="metadata=res://*/EDataModel.csdl|res://*/EDataModel.ssdl|res://*/EDataModel.msl;provider=System.Data.SqlServerCe.4.0;provider connection string=&quot;data source=|DataDirectory|\dbfile.sdf&quot;" 
             providerName="System.Data.SqlServerCe.4.0" />
    </connectionStrings>
</configuration>

生成 DbContext:

public partial class Entities : DbContext
{
    public Entities() : base("name=SomeDbFile")
    {
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        throw new UnintentionalCodeFirstException();
    }

    public DbSet<category> category { get; set; }
    public DbSet<item> item { get; set; }
    public DbSet<order> order { get; set; }
    public DbSet<order_line> order_line { get; set; }
}

当生成上下文时,它通常将连接字符串设置为与上下文相同的名称,因此值得检查那里没有问题。这样只要连接字符串名称正确,您就应该能够在客户端 web.config 中使用原始的 EF 连接字符串。

或者您可以注释掉 OnModelCreating 中的 "throw new UnintentionalCodeFirstException();" 行以使用普通连接字符串。

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        //comment out this line to allow for a normal connection string 
        throw new UnintentionalCodeFirstException(); 
    }

请检查这些链接以获取 EF 连接字符串

 https://msdn.microsoft.com/en-us/library/cc716756(v=vs.110).aspx  
https://msdn.microsoft.com/en-us/library/system.data.entityclient.entityconnectionstringbuilder.providerconnectionstring(v=vs.110).aspx
https://msdn.microsoft.com/en-us/library/system.data.entityclient.entityconnectionstringbuilder.providerconnectionstring(v=vs.110).aspx

尝试将连接字符串中的提供程序名称更改为 providerName="System.Data.EntityClient"

您可能忘记将连接设置放入主应用程序(实际上是 运行)app.config。

class 库项目中的

app.config 文件在 运行 时什么都不做,通常没有用。 (尽管某些 IDE 功能可能需要它们)