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

时间:2015-05-05 17:10:34

标签: c# winforms entity-framework sql-server-ce

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

  

在应用程序配置文件中找不到名为“SomeDbFile”的连接字符串。

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; }
}

2 个答案:

答案 0 :(得分:2)

您可能忘记将连接设置放在主应用程序(实际运行的东西)app.config中。

类库项目中的app.config文件在运行时不执行任何操作,通常无用。 (虽然某些IDE功能可能需要它们)

答案 1 :(得分:1)

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

或者你可以注释掉&#34;抛出新的UnintentionalCodeFirstException();&#34;从OnModelCreating到使用普通连接字符串的行。

        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 =&#34; System.Data.EntityClient&#34;

相关问题