Connection String not found WPF Entity Framework

Asked

Viewed 218 times

0

I am in a WPF project using some MVVM practices, I am using Entity Framework database first and the Connection string is already in the app.config, I have already followed all the procedures I have read in several questions but in the view appears this message in Datacontext

Erro na View

Below the connectionstring that is already in the app.config

 <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" />

The Context of the Database

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 I’ve done that haven’t worked:

  1. Remove "name" from Context;
  2. Put the Connection string in another file config and link to app.config;
  3. Comment on 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>
  • I need the contents of App1.config complete. Can you please update your question?

  • Good morning, the setup inside your app.config file is right ? You came to make a test to check if the connection with the Bank is being made ?

  • Already edited! Thanks for your help!

  • Why does the file call App1.config? You should just call yourself App.config. By doing the Build of his project, he goes on to call SeuProjeto.config.

  • Good morning Diego, so when I put the Command on a button my design time appears error of You are using code first in a database that was generated in databasefirst, something like that! Then I had to remove the Command from the button! To better explain this program is a time counter for equipment at a mall kiosk! So I just populated some data in the database! But for example in my viewModel I have a property called tbDataAtual that simply returns Datetime.Now and with this error even this is not returning! Thank you.

  • It’s because I thought it was some problem with the original project app.config I deleted it and put another one to test! But it didn’t work anyway!

Show 1 more comment

1 answer

0


This seems to me bad project setup. The configuration file is ok but apparently not being properly recognized.

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

1. Not using name=TimerEntities in the context builder

Would look like this:

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

2. Moving on to Connection string directly to the base builder

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 case of urgency.

  • Yes I had already used the first option and I receive the following information in the View

  • Severity Code Description Project File Line Suppression State Error The context is being used in Code First mode with code that was generated from an EDMX file for either Database First or Model First Development. This will not work correctly. To fix this problem do not remove the line of code that throws this Exception. If you Wish to use Database First or Model First, then make sure that the Entity Framework Connection string is included in the app.config or web.config of the start-up project.

  • CONTINUING... then make sure that it is an Entityconnection and not some other type of Dbconnection, and that you pass it to one of the base Dbcontext constructors that take a Dbconnection. To Learn more about Code First, Database First, and Model First see the Entity Framework Documentation

  • Yes, it happens because you used throw new UnintentionalCodeFirstException(); in the OnModelCreate. You can remove it now.

  • And I created the model in EF designer from database!

  • I had tried to take out too and there is another error! Severity Code Description Project File Line Suppression State Error A network-Related or instance-specific error occurred while establishing a Connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote Connections. (Provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified) Starttimer C: Users Angelica Documents visual studio 2015 Projects Starttimer Starttimer Views Caixawindow.xaml 11

  • I am using Sqlcompact, but I have tried also using Sqlite and it is the same error after all these steps!

  • This second error means that the application cannot connect to SQL Server CE. Instead, I would use SQL Server Localdb, which is the same principle and the support is better.

  • 1

    Well I think I’ve found the problem! Those days when I went to do the VS update 3 gave a b.ozinho and stopped the installation...so I had to repair the pc to get back... and I had not noticed that SQL SERVER DATA TOOLS was not installed! So I think that’s the mistake! I’m installing now and we’ll see if it works! Thanks for the tip and for taking the time to help me, I will research on Localdb.

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.