4

I m building a Form application in C#. I m using also Entity Framework to connect on database.

But if I try to start the application I have the following error:

//------------------------------------------------------------------------------
// <auto-generated>
//     Codice generato da un modello.
//
//     Le modifiche manuali a questo file potrebbero causare un comportamento imprevisto dell'applicazione.
//     Se il codice viene rigenerato, le modifiche manuali al file verranno sovrascritte.
// </auto-generated>
//------------------------------------------------------------------------------

namespace MSLab
{
    using System;
    using System.Data.Entity;
    using System.Data.Entity.Infrastructure;
    
    public partial class MsLabEntities2 : DbContext
    {
        public MsLabEntities2()
            : base("name=MsLabEntities2")
        {
        }
    
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            throw new UnintentionalCodeFirstException();
        }
    
        public virtual DbSet<Registrazioni> Registrazioni { get; set; }
        public virtual DbSet<RegistrazioniXAzione> RegistrazioniXAzione { get; set; }
    }
}

the error is on this line code:

: base("name=MsLabEntities2")

Unhandled exception of type 'System.TypeInitializationException' in EntityFramework.dll

Additional information: The type initializer of 'System.Data.Entity.Internal.AppConfig' threw an exception.

This is my App.config file

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <!-- 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>
  <appsettings>
    <WpfApplication1.Properties.Settings>
      <setting name="inactivity_interval" serializeAs="String">
        <value>10</value>
      </setting>
      <setting name="maximumHeightPopUp" serializeAs="String">
        <value>260</value>
      </setting>
      <setting name="horizontalArrowsHeight" serializeAs="String">
        <value>35</value>
      </setting>
      <setting name="modelsListHeight" serializeAs="String">
        <value>100</value>
      </setting>
    </WpfApplication1.Properties.Settings>
  </appsettings>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
  <connectionStrings>
    <add name="MsLabEntities" connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=TELESIO\SQLSERVER;initial catalog=MsLab;persist security info=True;user id=sa;password=Er$sult12345;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
    <add name="MsLabEntities1" connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=TELESIO\SQLSERVER;initial catalog=MsLab;user id=sa;password=Er$sult12345;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
    <add name="MsLabEntities2" connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=TELESIO\SQLSERVER;initial catalog=MsLab;user id=sa;password=Er$sult12345;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
</configuration>

This is my packages.config file

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="EntityFramework" version="6.1.3" targetFramework="net45" />
  <package id="EntityFramework.it" version="6.1.3" targetFramework="net45" />
</packages>
Francesco Bonizzi
  • 5,142
  • 6
  • 49
  • 88
bircastri
  • 2,169
  • 13
  • 50
  • 119

1 Answers1

0

Try updating the entityFramework part of the app.config to this

  <entityFramework>
  <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework">
    <parameters>
      <parameter value="<My Connection String>" />
    </parameters>
  </defaultConnectionFactory>
</entityFramework>
Daniel Björk
  • 2,475
  • 1
  • 19
  • 26
  • I pretty sure that this wasn't the issue. EF should run without this `` section and (on Sql Server) even without the entire `` section. It's more likely that OP unwittingly fixed another error in the config file while trying to apply this "fix". – Gert Arnold Jul 23 '20 at 12:52
  • @GertArnold I think it was the lack of Connection String in his App.Config that did it. – Daniel Björk Jul 23 '20 at 13:06
  • There is no "lack of connection string" in the config file. The context constructor refers to an existing connection string. – Gert Arnold Jul 23 '20 at 13:11
  • The error was in ``, see my comment above. Somewhere in the process OP fixed that. – Gert Arnold Jul 23 '20 at 13:12
  • OK, Well then he needs to shed some light on it. – Daniel Björk Jul 23 '20 at 13:12