C#TypeInitializationException在自动生成的代码中未处理

时间:2016-05-14 15:56:15

标签: c# .net entity-framework

我正在我的数据库中保存一个对象(实体),如下所示:

ctx = new UserEntities();

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

ctx.Users.Add(userDB);

然而,在我看来,自动生成的代码抛出TypeInitializationException,这看起来很奇怪。这是投掷它的类:

//------------------------------------------------------------------------------
// <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; }
    }
}

例外只说:

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'.

我一直在调查几个小时没有成功,任何提示? 提前致谢

更新:APP.CONFIG文件:

<?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>

1 个答案:

答案 0 :(得分:0)

我发现两个同名的DB之间存在某种冲突。在APP.CONFIG中评论这些行就可以了:

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