0

I am saving an object (entity) in my database, like this:

ctx = new UserEntities();

Users userDB = new Users();
userDB.name = user.firstName;
userDB.surname = user.surname;
userDB.phone = user.phone;              

ctx.Users.Add(userDB);

However, a TypeInitializationException is being thrown from the autogenerated code, which is something that seems weird, in my humble opinion. This is the class throwing it:

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated from a template.
//
//     Manual changes to this file may cause unexpected behavior in your application.
//     Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

namespace WindowsFormsApplication1
{
    using System;
    using System.Data.Entity;
    using System.Data.Entity.Infrastructure;

    public partial class UserEntities : DbContext
    {
        public UserEntities()
            : base("name=UserEntities") //OBS: THIS LINE THAT THROWS THE EXCEPTION
        {
        }

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

        public virtual DbSet<Users> Users { get; set; }
    }
}

The exception only says:

An unhandled exception of type 'System.TypeInitializationException' occurred in EntityFramework.dll

Additional information: Se produjo una excepción en el inicializador de tipo de 'System.Data.Entity.Internal.AppConfig'.

I have been investigating for hours without success, any hint? Thanks in advance

UPDATE: 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 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
  </startup>
  <connectionStrings>
    <add name="HerculesDatabaseEntities" connectionString="metadata=res://*/csdl|res://*/ssdl|res://*/msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=(LocalDB)\MSSQLLocalDB;attachdbfilename=|DataDirectory|\HerculesDatabase.mdf;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
    <add name="userEntities" connectionString="metadata=res://*/UsersModel.csdl|res://*/UsersModel.ssdl|res://*/UsersModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=(LocalDB)\MSSQLLocalDB;attachdbfilename=|DataDirectory|\HerculesDatabase.mdf;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
    <add name="HerculesDatabaseEntities1" connectionString="metadata=res://*/UsersModel.csdl|res://*/UsersModel.ssdl|res://*/UsersModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=(LocalDB)\MSSQLLocalDB;attachdbfilename=|DataDirectory|\HerculesDatabase.mdf;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
    <add name="UserEntities" connectionString="metadata=res://*/UserModel.csdl|res://*/UserModel.ssdl|res://*/UserModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=(LocalDB)\MSSQLLocalDB;attachdbfilename=|DataDirectory|\HerculesDatabase.mdf;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="mssqllocaldb" />
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
</configuration>
leppie
  • 115,091
  • 17
  • 196
  • 297
stack man
  • 2,303
  • 9
  • 34
  • 54
  • 1
    You have some problems in your configuration (app.config) file. See here for similar exception (thought your problem might be different) - http://stackoverflow.com/q/17585611/5311735 – Evk May 14 '16 at 16:04
  • Thanks, but is this possible when I didn´t touch the code? – stack man May 14 '16 at 16:07
  • 2
    Your config file doesn't have a connection string, or the connection string is incorrectly formatted. Also, check the `InnerException` or other properties of that exception, they should tell you the underlying problem. – Lasse V. Karlsen May 14 '16 at 16:09
  • 1
    Sure, as you see :) If you provide more details (for example - your app.config file) we might help you more. – Evk May 14 '16 at 16:09
  • Thanks for your answers, I added app.config file – stack man May 14 '16 at 16:21
  • You'll need to do something about the way you diagnose exceptions. For exceptions like this it is absolutely critical to inspect the InnerException so you can see the core reason. This is supposed to be taken care of by the Exception Assistant. If it doesn't let you look at the exception details, a bug in VS2015, then temporarily use Tools > Options > Debugging > General > tick the "Use Managed Compatibility Mode" option. – Hans Passant May 14 '16 at 17:39

1 Answers1

0

I discovered that there was some kind of conflict between two DBs with the same name. Commenting these lines in APP.CONFIG did the trick:

<!--
<add name="HerculesDatabaseEntities" connectionString="metadata=res://*/csdl|res://*/ssdl|res://*/msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=(LocalDB)\MSSQLLocalDB;attachdbfilename=|DataDirectory|\HerculesDatabase.mdf;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
    <add name="userEntities" connectionString="metadata=res://*/UsersModel.csdl|res://*/UsersModel.ssdl|res://*/UsersModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=(LocalDB)\MSSQLLocalDB;attachdbfilename=|DataDirectory|\HerculesDatabase.mdf;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
-->
stack man
  • 2,303
  • 9
  • 34
  • 54