TransWikia.com

Error to start application after insert Entity Framework

Stack Overflow Asked by bircastri on November 7, 2021

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=TELESIOSQLSERVER;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=TELESIOSQLSERVER;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=TELESIOSQLSERVER;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>

One Answer

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>

Answered by Daniel Björk on November 7, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP