Connection String not found WPF Entity Framework

0

I'm in a WPF project using some MVVM practices, I'm using the Entity Framework database first and the connection string is already in the app.config, I've already followed all the procedures I've read on several issues but in the view this message appears in the DataContext

Belowtheconnectionstringthatisalreadyintheapp.config

<addname="TimerEntities" connectionString="metadata=res://*/Timer.csdl|res://*/Timer.ssdl|res://*/Timer.msl;provider=System.Data.SqlServerCe.4.0;provider connection string=&quot;Data Source=C:\Users\Angelica\Documents\DB\Timer.sdf;Max Database Size=4091&quot;"
 providerName="System.Data.EntityClient" />

The Database Context

using System;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;

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

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

    public virtual DbSet<CAIXA> CAIXAS { get; set; }
    public virtual DbSet<CLIENTE> CLIENTES { get; set; }
    public virtual DbSet<ORDEM> ORDEM { get; set; }
    public virtual DbSet<PACOTE> PACOTES { get; set; }
    public virtual DbSet<TEMPO> TEMPOS { get; set; }
}

Procedures that I have already done and did not work:

  • Remove the "name" from the Context;
  • Put the connection string into another config file and link to app.config ;
  • Comment throw new UnintentionalCodeFirstException(); ;
  • <?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=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
        <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
      </configSections>
        <connectionStrings>
          <add name="TimerEntities" connectionString="metadata=res://*/Timer.csdl|res://*/Timer.ssdl|res://*/Timer.msl;provider=System.Data.SqlServerCe.4.0;provider connection string=&quot;Data Source=C:\Users\Angelica\Documents\DB\Timer.sdf;Max Database Size=4091&quot;"
         providerName="System.Data.EntityClient" />
      </connectionStrings>
      <startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6" />
      </startup>
      <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>
    </configuration>
    
        
    asked by anonymous 14.07.2016 / 14:27

    1 answer

    0

    This looks like a bad design setup. The configuration file is ok but apparently is not being properly recognized.

    There are a few things you can do to get around the problem quickly:

    1. Not using name=TimerEntities in context constructor

    It would look like this:

    public TimerEntities()
        : base("TimerEntities")
    {
    }
    

    2. Passing the connection string directly to the base constructor

    It would look like this:

    public TimerEntities()
        : base("metadata=res://*/Timer.csdl|res://*/Timer.ssdl|res://*/Timer.msl;provider=System.Data.SqlServerCe.4.0;provider connection string=&quot;Data Source=C:\Users\Angelica\Documents\DB\Timer.sdf;Max Database Size=4091&quot;")
    {
    }
    

    The latter is not exactly recommended. Only use in an emergency.

        
    14.07.2016 / 14:55