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

时间:2014-07-28 18:10:24

标签: c# .net entity-framework wcf

我创建了一个实体框架DLL EMPDAL,它指向Northwnd数据库的Employees表。以下是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();
    }
}}

以下是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>

以下是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);
    }
}}

以下是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>

当客户端使用WCF服务时,以及当它尝试执行时使用(DbEntities dbContext = new DbEntities())&#34;它转到了

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

并抛出异常

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

1 个答案:

答案 0 :(得分:0)

这是非常通用的异常,因此在编辑器中尝试阅读异常的全部细节,然后解决它。 对我来说,这是由于在webconfig文件中编写的实体框架版本和实际文件包含,有区别,我还必须删除我的项目的Web配置文件中的provider标签