简体   繁体   English

未处理typeinitializationexception。'System.Data.Entity.Internal.AppConfig'的类型初始值设定项引发了异常

[英]typeinitializationexception was unhandled .The type initializer for 'System.Data.Entity.Internal.AppConfig' threw an exception

I have created a Entity Framework DLL EMPDAL which points to Employees Table of Northwnd database. 我创建了一个指向Northwnd数据库的Employees Table的Entity Framework DLL EMPDAL。 Below is the code for Entity framework 以下是实体框架的代码

namespace EmpDAL{
public class EmplooyeeData
{
    public static List<Employee> GetEmployees( int EmployeeId)
    {
        using (DbEntities dbContext = new DbEntities())
        {
            return dbContext.Employees.Where(x => x.EmployeeID == EmployeeId).ToList();
        }
    }
    public static void SaveEmployee(Employee emp)
    {
        DbEntities dbContext = new DbEntities();
        dbContext.Employees.Add(emp);
        dbContext.SaveChanges();
    }
}}

Below is the Appconfig file of EMPDAL 以下是EMPDAL的Appconfig文件

<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>
  <connectionStrings>
    <add name="DbEntities" connectionString="metadata=res://*/EmployeeModel.csdl|res://*/EmployeeModel.ssdl|res://*/EmployeeModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=localhost;initial catalog=NorthWnd;user id=sa;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
  </entityFramework>
</configuration>

Below is the code for the WCF service 以下是WCF服务的代码

namespace EmployeeService{

public class EmployeeService : IEmployeeService
{
    public List<EmpDAL.Employee> GetEmployees(int Empid)
    {
        return EmpDAL.EmplooyeeData.GetEmployees(Empid);
    }

    public void SaveChanges(EmpDAL.Employee emp)
    {
        EmpDAL.EmplooyeeData.SaveEmployee(emp);
    }
}}

Below is the AppConfig for WCF EmployeeService 以下是WCF EmployeeService的AppConfig

 <configuration>
<system.serviceModel>
    <behaviors>
        <serviceBehaviors>
            <behavior name="">
                <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
                <serviceDebug includeExceptionDetailInFaults="false" />
            </behavior>
        </serviceBehaviors>
    </behaviors>
    <services>
        <service name="EmployeeService.EmployeeService">
            <endpoint address="" binding="basicHttpBinding" contract="EmployeeService.IEmployeeService">
                <identity>
                    <dns value="localhost" />
                </identity>
            </endpoint>
            <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
            <host>
                <baseAddresses>
                    <add baseAddress="http://localhost:8733/Design_Time_Addresses/EmployeeService/EmployeeService/" />
                </baseAddresses>
            </host>
        </service>
    </services>
</system.serviceModel>  <configSections>
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />  </configSections>  <connectionStrings>
<add name="DbEntities"connectionString="metadata=res://*/EmployeeModel.csdl|res://*/EmployeeModel.ssdl|res://*/EmployeeModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=localhost;initial catalog=NorthWnd;user id=sa;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" /></connectionStrings>  <entityFramework>   <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory,EntityFramework" /> </entityFramework></configuration>

When client uses the WCF service it and when it tries to execute "using (DbEntities dbContext = new DbEntities())" it goes to 当客户端使用WCF服务时,并尝试执行“使用(DbEntities dbContext = new DbEntities())”时,它将转到

 public partial class DbEntities : DbContext    {
    public DbEntities()
        : base("name=DbEntities")

and throws exception 并引发异常

typeinitializationexception was unhandled .The type initializer for 'System.Data.Entity.Internal.AppConfig' threw an exception.

It is very generic exception so in your editor try reading the whole details of the exception and then resolve it. 这是非常通用的异常,因此请在您的编辑器中尝试阅读异常的全部细节,然后加以解决。 For me, it was due to the entity framework version written in webconfig file and actual file included ,there was difference and also i had to remove providers tag in web config file of my project 对我来说,这是由于实体框架版本写在webconfig文件中,并且包含实际文件,两者之间存在差异,而且我不得不在我的项目的web配置文件中删除了provider标签

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 &#39;System.Data.Entity.Internal.AppConfig&#39;的类型初始化程序在子网站上引发了一个例外 - The type initializer for 'System.Data.Entity.Internal.AppConfig' threw an exception on a Sub Website &#39;System.Data.Entity.Internal.AppConfig&#39;的类型初始值设定项引发了Windows XP的异常 - the type initializer for 'System.Data.Entity.Internal.AppConfig' threw an exception for Windows XP SQL Server Express “'system.data.entity.internal.appconfig' 的类型初始化程序引发异常”错误 - SQL Server Express “the type initializer for 'system.data.entity.internal.appconfig' threw an exception” error 在 .NET4.5 上安装 EntityFramework5.0.0 后错误:“System.Data.Entity.Internal.AppConfig”的类型初始化程序在运行时引发异常 - After installing EntityFramework5.0.0 on .NET4.5 Error: The type initializer for 'System.Data.Entity.Internal.AppConfig' threw an exception at Runtime System.TypeInitializationException:&#39;Tips&#39;的类型初始值设定项引发了异常 - System.TypeInitializationException: The type initializer for 'Tips' threw an exception System.TypeInitializationException: &#39;Bid&#39; 的类型初始值设定项引发异常。 - System.TypeInitializationException: 'The type initializer for 'Bid' threw an exception.' System.TypeInitializationException:“SQLite.SQLiteConnection”的类型初始化程序引发异常 - System.TypeInitializationException: The type initializer for 'SQLite.SQLiteConnection' threw an exception System.TypeInitializationException:&#39;类型初始值设定项引发了异常。 - System.TypeInitializationException: 'The type initializer threw an exception.' System.Data.Entity.MigrateDatabaseToLatestVersion 的类型初始值设定项引发异常 - The type initializer for System.Data.Entity.MigrateDatabaseToLatestVersion threw an exception 'System.Data.Entity.Migrations.DbMigrationsConfiguration`1' 的类型初始值设定项抛出异常 - The type initializer for 'System.Data.Entity.Migrations.DbMigrationsConfiguration`1' threw an exception
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM